Hello.
In a table, I would like a column to show the last date/time that the calculations in the neighboring column were updated. In particular StateBuilder is a calculated column in my table. The neighboring column SB_Time should populate with the date/time whenever a cell in StateBuilder has a new result calculated.
My below code accomplishes this exactly as desired, with one problem. After the workbook is opened, the first calculation update to StateBuilder does not update SB_Time. All subsequent updates to StateBuilder successfully update SB_Time as desired. I can’t determine why the first update never works and I am seeking a solution.
If I remove the “IsEmpty(previousStates)” condition from the below code, I get a Type Mismatch error at line “previousState = previousStates(i, 1)” and I can’t determine why.
Some notes
• StateBuilder is a remotely calculated column so Worksheet_Change and Worksheet_SelectionChange events will not work for this purpose
• The table name is MasterState
• The worksheet name is UseState
I appreciate any suggestions. While it may appear otherwise, I have the comprehension of a small child. So if you are inclined to offer any suggestions or instructions, please spell them out in simple and deliberative terms.
In a table, I would like a column to show the last date/time that the calculations in the neighboring column were updated. In particular StateBuilder is a calculated column in my table. The neighboring column SB_Time should populate with the date/time whenever a cell in StateBuilder has a new result calculated.
My below code accomplishes this exactly as desired, with one problem. After the workbook is opened, the first calculation update to StateBuilder does not update SB_Time. All subsequent updates to StateBuilder successfully update SB_Time as desired. I can’t determine why the first update never works and I am seeking a solution.
If I remove the “IsEmpty(previousStates)” condition from the below code, I get a Type Mismatch error at line “previousState = previousStates(i, 1)” and I can’t determine why.
Some notes
• StateBuilder is a remotely calculated column so Worksheet_Change and Worksheet_SelectionChange events will not work for this purpose
• The table name is MasterState
• The worksheet name is UseState
I appreciate any suggestions. While it may appear otherwise, I have the comprehension of a small child. So if you are inclined to offer any suggestions or instructions, please spell them out in simple and deliberative terms.
VBA Code:
Private previousStates As Variant ' Declare a variant variable to store the previous states
Private Sub Worksheet_Calculate()
Dim rng As Range
Dim currentState As Variant
Dim i As Long
Set rng = Me.Range("MasterState[StateBuilder]") ' Assuming MasterState is a table
Application.EnableEvents = False ' Disable events to prevent recursive calls
If IsEmpty(previousStates) Then
' Store the previous states for the first time
previousStates = rng.Value
Else
' Loop through the StateBuilder column
For i = 1 To rng.Rows.Count
currentState = rng.Cells(i, 1).Value
previousState = previousStates(i, 1)
' Check if the current state is different from the previous state
If currentState <> previousState Then
' Update the SB_Time column with the current date and time
rng.Cells(i, 2).Value = Now()
End If
Next i
End If
' Update the previous states for the next iteration
previousStates = rng.Value
Application.EnableEvents = True ' Enable events again
End Sub