Automatically show date when particular result detected from Excel formula - excel

I am trying with VBA to get the current date on column H (Date).
In column F (Result)
If I manually type Preferred or Non-preferred.
After pressing Enter, today's date will be automatically put on column H (Date)
When I paste formula instead (which will consider data from column A-E to show result on its cell).
Even if the result gives Preferred or Non-preferred the date will not automatically show up.
Unless I press double-click and enter on each result cell then it will show up.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Dim St As String
St = "Preferred|Non-Preferred"
If Not Intersect(Columns("F"), Target) Is Nothing Then
Application.EnableEvents = False
For Each c In Intersect(Columns("F"), Target).Cells
If InStr(1, St, c.Value, vbTextCompare) >= 1 Then
Cells(c.Row, "H").Value = Date
Else
If IsEmpty(c) Then Cells(c.Row, "H").Value = ""
End If
Next c
Application.EnableEvents = True
End If
End Sub
Example

A Worksheet Change: Using Precedents
In column F is a formula that calculates dependent on the values of columns A:E (precedents). The code will trigger when the values in columns A:E are manually changed, possibly modifying the value in column H (see OP's description (requirements)).
The Select Case statement could be reduced to two cases: either it begins with Criteria (1) or it doesn't (Else).
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ClearError
Const sFirstCellAddress As String = "F2"
Const dCol As String = "H"
Const Criteria As String = "Preferred"
Dim srg As Range
With Me.Range(sFirstCellAddress)
Set srg = .Resize(Me.Rows.Count - .Row + 1)
End With
Dim irg As Range: Set irg = Intersect(srg.Precedents, Target)
If irg Is Nothing Then Exit Sub
Set srg = Intersect(irg.EntireRow, Columns(srg.Column))
Dim drg As Range: Set drg = srg.EntireRow.Columns(dCol)
Application.EnableEvents = False
Dim dCell As Range
Dim n As Long
For Each dCell In drg.Cells
n = n + 1
Select Case InStr(1, CStr(srg.Cells(n).Value), Criteria, vbTextCompare)
Case 0
dCell.Value = Empty ' criteria not found
Case 1
dCell.Value = Date ' begins with criteria
Case Else
dCell.Value = Empty ' contains criteria
End Select
Next dCell
SafeExit:
If Not Application.EnableEvents Then Application.EnableEvents = True
ProcExit:
Exit Sub
ClearError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub

Related

VBA Code to repeat a function in rows 2-100

I have little experience with VBA code and I am trying to get this code to repeat for rows 2-100. The issue I have found vs other codes that repeat in rows is that mine has multiple end arguments and I'm not sure how to account for this. Any help is greatly appreciated.
Private Sub Worksheet_SelectionChange2(ByVal Target As Range)
a = Date
b = 2
If Cells(b, 3).Value <> Blank Then
If Cells(b, 2).Value = Blank Then
Cells(b, 2).Value = a
Exit Sub
End If
If Cells(b, 2).Value < a Then
Exit Sub
End If
Cells(b, 2).Value = a
End If
End Sub
This is what I'm working with. I tried to make the cell reference a variable that I could count up but whatever I tried it didn't work.
Edit: Sorry for lack of clarification. The code is supposed to put today's date in B2 when C2 goes from being empty to having anything in it. It also prevents the date from being changed if there is already a date there, even if C2 is cleared. I am trying to extend it so that rather than just C2 and B2 it is C2-C100 and then corresponding B2-B100.
Edit 2: C2 is being changed by a manual input. The purpose is to have someone input data into C2 (and the rest of the row) and for the date to be automatically entered and locked so they cannot change it and I can see when the data was inputted.
A Worksheet Change (Timestamp)
This will only work if the values in column C are modified manually i.e. by manually entering, by copy/pasting, and by writing via VBA.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ClearError
Const fRow As Long = 2
Const sCol As String = "C"
Const dCol As String = "B"
Dim scrg As Range: Set scrg = Columns(sCol).Resize(Rows.Count - fRow + 1)
Dim srg As Range: Set srg = Intersect(scrg, Target)
If srg Is Nothing Then Exit Sub
Dim drg As Range
Dim dCell As Range
Dim sCell As Range
For Each sCell In srg.Cells
Set dCell = sCell.EntireRow.Columns(dCol)
If Len(CStr(dCell.Value)) = 0 Then
If drg Is Nothing Then
Set drg = dCell
Else
Set drg = Union(drg, dCell)
End If
End If
Next sCell
' All cells already contain a date.
If drg Is Nothing Then Exit Sub
Dim dDate As Date: dDate = Now ' after prooving that it works, use 'Date'
' To prevent retriggering this event and any other event while writing.
Application.EnableEvents = False
' Write in one go.
drg.Value = dDate
SafeExit:
' Enable events 'at any cost'.
If Not Application.EnableEvents Then Application.EnableEvents = True
Exit Sub
ClearError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub

Private sub update date automatically when value in a cell changes

Im trying to automatically update current date in cell T when text in cell Q is "won" and a value in cell AM is > 0. I tried the code below and it is working if first the value in cell is > 0 and then you update the text in cell Q BUT if you do it in another way (first update cell Q and secondly the value in cell AM) the date doesn't appear in cell T.
Any idea, what Im I missing?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, [Q:Q]) Is Nothing Then
If UCase(Target) = UCase("won") And Target.Offset(, 22) > 0 Then
Target.Offset(, 2) = Int(Now())
End If
End If
End sub
Your code only checks for changes in Q therefore the update does not take place if you change AM first.
My solution has three parts:
use constants for the columns - in case there are changes to the sheet layout you only have to make adjustments here
worksheet_change: only check if one of the columns is affected then call the according sub - by that the reader of the code immediately understands what is going on here
the main routine that inserts the date if condition is met or removes the date if not (maybe you want to adjust this)
Option explicit
Private Const colStatus As String = "Q"
Private Const colValue As String = "AM"
Private Const colDateWon As String = "S"
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Set c = Target.Cells(1, 1)
If c.Column = Me.Columns(colStatus).Column Or c.Column = Me.Columns(colValue).Column Then
updateDateWon c.row
End If
End Sub
Private Sub updateDateWon(row As Long)
'--> adjust the name of the sub to your needs
Dim valueToInsert As Variant
With Me
If .Range(colStatus & row) = "won" And .Range(colValue & row) > 0 Then
valueToInsert = Int(Now)
Else
'reset the date in case conditions are not met
valueToInsert = vbNullString
End If
Application.EnableEvents = False 'disable events so that change-event isn't called twice
.Range(colDateWon & row) = valueToInsert
Application.EnableEvents = True
End With
End Sub
A Worksheet Change Applied to Two Non-Adjacent Columns
You need to monitor columns Q and AM for changes.
You need to account for Target being multiple adjacent and non-adjacent cells.
You need to disable events when writing to the worksheet containing this code to not retrigger this event (or trigger any other events).
It is good practice to ensure the re-enabling of events (by using error-handling).
You can combine the cells to be written to (dCell) into a range (drg) and write the stamp in one go.
Int(Now()) or Int(Now) is actually Date.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ClearError
Const sColsAddress As String = "Q:Q,AM:AM"
Const dCol As String = "T"
Const fRow As Long = 2 ' (e.g. 2 for excluding headers in the first row)
Const sCriteria As String = "won"
Dim srg As Range
With Range(sColsAddress)
Set srg = Intersect(.Cells, Rows(fRow).Resize(Rows.Count - fRow + 1))
End With
Dim sirg As Range: Set sirg = Intersect(srg, Target)
If sirg Is Nothing Then Exit Sub
Dim sirg1 As Range: Set sirg1 = Intersect(sirg.EntireRow, srg.Areas(1))
Dim siCol2 As Long: siCol2 = srg.Areas(2).Column
'Dim dirg As Range: Set dirg = sirg1.EntireRow.Columns(dCol) ' not used
Dim siCell1 As Range
Dim siValue2 As Variant
Dim drg As Range
For Each siCell1 In sirg1.Cells
If StrComp(CStr(siCell1.Value), sCriteria, vbTextCompare) = 0 Then
siValue2 = siCell1.EntireRow.Columns(siCol2).Value
If IsNumeric(siValue2) Then
If siValue2 > 0 Then
If drg Is Nothing Then
Set drg = siCell1.EntireRow.Columns(dCol)
Else
Set drg = Union(drg, siCell1.EntireRow.Columns(dCol))
End If
End If
End If
End If
Next siCell1
If Not drg Is Nothing Then
' Prevent retriggering the event when writing to the worksheet.
Application.EnableEvents = False
drg.Value = Now ' only after testing, use 'dDate = Date'
End If
SafeExit:
' Enable events 'at all cost'.
If Not Application.EnableEvents Then Application.EnableEvents = True
Exit Sub
ClearError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub

How can I get this Excel VBA to run for all rows, instead of just the first row?

So darn new to VBA, almost got this working. I'm trying to reset some Data Validation lists so that if someone changes a Country selection, then a couple of cells will reset. Example, if I pick USA then I want the corresponding State and Shift column to display "Please select..." and if the user changes the country to something other than USA then it doesn't say anything in the State column, only in the Shift column. I got this working but it only runs for the first row. I'm not sure if my range is wrong or if I'm supposed to loop, both of which I'm totally ignorant on.
Option Explicit
'The way this works is if the Payroll Country changes then the sub selections
'of State and Shift should reset based on if the country is USA
'Payroll Country = A column
'State = X column
'Shift = Y column
Private Sub Worksheet_Change(ByVal Target As Range)
'"If Target.Count > 1 Then Exit Sub" is the VBA code to prevent an error if user highlights the range and deletes the data
If Target.Count > 1 Then Exit Sub
If Target.Address = "$A$6" And Target.Value = "USA" Then
Range("X6").Value = "Please select..."
Range("Z6").Value = "Please select..."
ElseIf Target.Address = "$A$6" And Target.Value <> "USA" Then
Range("X6").Value = ""
Range("Z6").Value = "Please select..."
End If
End Sub
Try this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub 'CountLarge handles larger ranges...
'check Target column and row...
If Target.Column = 1 and Target.Row >= 6 Then
With Target.EntireRow
.Columns("X").Value = IIf(Target.Value = "USA", _
"Please select...", "")
.Columns("Z").Value = "Please select..."
End With
End If
End Sub
A Worksheet Change
If you out-comment the line Application.EnableEvents = False you will notice that there will be more Debug.Print lines (one more for USA, two more for non-USA) i.e. after writing (crg.Value = InitialString), the code calls itself (actually the worksheet change event calls the InitializeCountry procedure) again but 'luckily' exits after the line If irg Is Nothing Then Exit Sub since it is writing to a non-intersecting range, but nevertheless debug-printing the Source Range Address once more, before it continues with the line Debug.Print "Criteria Range (Both Columns): " & crg.Address(0, 0). This should help you to fully understand why the disabling of events is necessary.
Out-comment or delete the range addresses related Debug.Print lines when done testing because they're slowing down the code.
Sheet Module e.g. Sheet1
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
InitializeCountry Target
End Sub
Standard Module e.g. Module1
Option Explicit
Sub InitializeCountry( _
ByVal Target As Range)
' Declare constants.
Const SourceFirstCellAddress As String = "A6"
Const StateColumn As String = "X"
Const ShiftColumn As String = "Z"
Const CriteriaString As String = "USA"
Const InitialString As String = "Please select..."
' Read.
Dim ws As Worksheet: Set ws = Target.Worksheet
Dim sfCell As Range: Set sfCell = ws.Range(SourceFirstCellAddress)
Dim srg As Range: Set srg = sfCell.Resize(ws.Rows.Count - sfCell.Row + 1)
Debug.Print "Source Range Address: " & srg.Address(0, 0)
Dim irg As Range: Set irg = Intersect(srg, Target) ' Intersect Range
If irg Is Nothing Then Exit Sub
Debug.Print "Intersect Range Address: " & irg.Address(0, 0)
' ShiftColumn
Dim crg As Range ' Criteria Range
Set crg = irg.EntireRow.Columns(ShiftColumn)
Debug.Print "Criteria Range (ShiftColumn): " & crg.Address(0, 0)
' StateColumn
Dim erg As Range ' Empty Range
Dim iCell As Range ' Current Intersect Cell
For Each iCell In irg.Cells
If iCell.Value = CriteriaString Then
Set crg = Union(crg, iCell.EntireRow.Columns(StateColumn))
Else
If erg Is Nothing Then
Set erg = iCell.EntireRow.Columns(StateColumn)
Else
Set erg = Union(erg, iCell.EntireRow.Columns(StateColumn))
End If
End If
Next iCell
' Write.
Application.ScreenUpdating = False
' This is crucial to not retrigger the event procedure when writing!
Application.EnableEvents = False
On Error GoTo ClearError
crg.Value = InitialString
Debug.Print "Criteria Range (Both Columns): " & crg.Address(0, 0)
If Not erg Is Nothing Then
erg.ClearContents ' erg.Value = Empty
Debug.Print "Empty Range (StateColumn): " & erg.Address(0, 0)
End If
SafeExit:
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
ClearError:
' Don't uncomment this line!
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Resume SafeExit
End Sub
' Test multi-range (only possible with VBA).
Sub InitializeCountryTEST()
Dim rg As Range: Set rg = Range("A6:A20,A31:A50,A61:A100")
rg.Value = "USA"
End Sub
' Debug.Print result for 'USA' (no 'Empty Range'):
'Source Range Address: A6:A1048576
'Intersect Range Address: A6:A20,A31:A50,A61:A100
'Criteria Range(ShiftColumn): Z6:Z20
'Criteria Range (Both Columns): Z6:Z20,X6:X20,X31:X50,X61:X100
' Debug.Print result otherwise:
'Source Range Address: A6:A1048576
'Intersect Range Address: A6:A20,A31:A50,A61:A100
'Criteria Range(ShiftColumn): Z6:Z20
'Criteria Range (Both Columns): Z6:Z20
'Empty Range (StateColumn): X6:X20,X31:X50,X61:X100

Auto-Updated Validated Cell When Source Value Changes

I'm trying to update cells that have data validation restrictions on them automatically.
For example - Sheet1 has below column (Column E):
Package Identifier
A
B
C
where the values are taken from the same named column (Column D) in Sheet2.
The below code works for MANUAL changes only
Sheet2 Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Dim count_cells As Integer
Dim new_value As String
Dim old_value As String
Dim rng As Range
For count_cells = 1 To Range("D1").CurrentRegion.Rows.Count - 1
Set rng = Worksheets("Sheet1").Range("E3:E86")
If Intersect(Target, Range("D" & count_cells + 1)) Is Nothing Then
Else
Application.EnableEvents = False
new_value = Target.Value
Application.Undo
old_value = Target.Value
Target.Value = new_value
rng.Replace What:=old_value, Replacement:=new_value, LookAt:=xlWhole
Target.Select
End If
Next count_cells
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
So, if i manually change value B to Z, all the corresponding values that were B on Sheet1 now change to Z. The problem is, Package Identifier on Sheet2 is dictated by concatenating other columns
=CONCATENATE(B35, "-", "Package", "-", TEXT(C35, "#000"))
This piece of code breaks when trying to use it with the above formula. How can i make this set of code trigger on this formula based output?
Assuming this is how the Validation sheet looks
and this is how the Source sheet looks
Let's say user selects first option in Validation sheet.
Now go back to Source sheet and change 1 to 2 in cell C2.
Notice what happens in Validation sheet
If this is what you are trying then based on the file that you gave, test this code.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim aCell As Range
Dim NewSearchValue As String
Dim OldSearchValue As String
Dim NewArrayBC As Variant
Dim OldArrayA As Variant, NewArrayA As Variant
Dim lRow As Long, PrevRow As Long
On Error GoTo Whoa
Application.EnableEvents = False
If Not Intersect(Target, Range("B:C")) Is Nothing Then
lRow = Range("A" & Rows.Count).End(xlUp).Row
'~~> Store new values from Col A, B and C in an array
NewArrayBC = Range("B1:C" & lRow).Value2
NewArrayA = Range("A1:A" & lRow).Value2
Application.Undo
'~~> Get the old values from Col A
OldArrayA = Range("A1:A" & lRow).Value2
'~~> Paste the new values in Col B/C
Range("B1").Resize(UBound(NewArrayBC), 2).Value = NewArrayBC
'~~> Loop through the cells
For Each aCell In Target.Cells
'~~> Check if the prev change didn't happen in same row
If PrevRow <> aCell.Row Then
PrevRow = aCell.Row
NewSearchValue = NewArrayA(aCell.Row, 1)
OldSearchValue = OldArrayA(aCell.Row, 1)
Worksheets("Validation").Columns(2).Replace What:=OldSearchValue, _
Replacement:=NewSearchValue, Lookat:=xlWhole
End If
Next aCell
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
A different approach from Sid's...
Instead of updating values in the DV cells when the source range changes, this replaces the selected value with a link to the matching cell in the DV source range.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngV As Range, rng As Range, c As Range, rngList As Range
Dim f As Range
On Error Resume Next
'any validation on this sheet?
Set rngV = Me.Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo 0
If rngV Is Nothing Then Exit Sub 'no DV cells...
Set rng = Application.Intersect(rngV, Target)
If rng Is Nothing Then Exit Sub 'no DV cells in Target
For Each c In rng.Cells
If c.Validation.Type = xlValidateList Then 'DV list?
Set rngList = Nothing
On Error Resume Next
'see if we can get a source range
Set rngList = Evaluate(c.Validation.Formula1)
On Error GoTo 0
If Not rngList Is Nothing Then
Application.EnableEvents = False
'find cell to link to
Set f = rngList.Find(c.Value, LookIn:=xlValues, lookat:=xlWhole)
If Not f Is Nothing Then
Application.EnableEvents = False
c.Formula = "='" & f.Parent.Name & "'!" & f.Address(0, 0)
Application.EnableEvents = True
End If
Else
Debug.Print "No source range for " & c.Address
End If
End If
Next c
End Sub

Record Date values based on another cell's value

I am a beginner in VBA.
I have a Column "A" can have multiple values, of which 2 are "Impact Assessed" or "Ready for retesting".
Problem Statement - I want to record the dates when cell's value is changed to Impact Assessed and Ready for Retesting in 2 separate columns - Column B and Column C, respectively.
Below is my code -
Private Sub Worksheet_Calculate()
Dim rng As Range
If Target.CountLarge > 1 Then Exit Sub
Set rng = Application.Intersect(Me.Range("AA:AA"), Target)
If Not rng Is Nothing Then
Select Case (rng.Value)
Case "2 - Impact Assessed": rng.Offset(0, 1).Value = Date
Case "4 - Ready for retesting": rng.Offset(0, 2).Value = Date
End Select
End If
End Sub
I have made the code as versatile as possible. Just change the constants and, if need be, the search criteria in the Criteria variable to suit your worksheet and you can change your sheet as you like without needing to modify the code.
Private Sub Worksheet_Change(ByVal Target As Range)
' 040
Const TriggerClm As String = "A" ' change to suit
Const WriteToClm As String = "B" ' the second one is next to this
Dim Rng As Range ' working range
Dim C As Long ' WriteToClm
Dim Criteria() As String ' selected values from TriggerClm
Dim i As Integer ' index to Criteria()
' don't respond to changes of multiple cells such as Paste or Delete
If Target.CountLarge > 1 Then Exit Sub
' respond to changes in cells from row 2 to
' one cell below the last used row in the trigger column
Set Rng = Range(Cells(2, TriggerClm), _
Cells(Rows.Count, TriggerClm).End(xlUp).Offset(1))
If Not Application.Intersect(Rng, Target) Is Nothing Then
' intentionally all lower case because comparison
' is carried out case insensitive
' First item's date is in WriteToClm
Criteria = Split("impact assessed,ready for retesting", ",")
For i = UBound(Criteria) To 0 Step -1
If StrComp(Target.Value, Criteria(i), vbTextCompare) = 0 Then Exit For
Next i
' i = -1 if no match was found
If i >= 0 Then
C = Columns(WriteToClm).Column + i
Cells(Target.Row, C).Value = Date
End If
End If
End Sub
You can use something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, c As Range
'If Target.CountLarge > 1 Then Exit Sub
Set rng = Application.Intersect(Me.Range("A:A"), Target)
If Not rng Is Nothing Then
For Each c in rng.cells
Select Case LCase(c.Value)
Case "impact assessed": c.Offset(0, 1).Value = Date
Case "ready": c.Offset(0, 2).Value = Date
End Select
Next c
End If
End Sub
FYI = Range(Target.Address) is the same (in this case) as Target - no need to get the address just to turn that back into a range.

Resources