Selected Answer
Sunil
In the first revised (.xlsm) file, I've defined two variables at the Workbook level (so their values are retained), followed by an event macro to captures the values in column D of the "Remote values" sheet when the file is launched:
Dim LastVals As Variant, LstRw As Long
Private Sub Workbook_Open()
' capture depth of sheet (col A) and current values (col D)
With Sheet1
LstRw = .Range("A" & Rows.Count).End(xlUp).Row
LastVals = .Range("D1:D" & LstRw).Value
End With
End Sub
Note that it populates an array LastVals with values but converts your "TRUE" entries to Boolean True (so I need to use CStr to convert it back to a string in the macro below)
If the second (.xlsx) file is open and a cell in column B is changed to "Missing" (so column D becomes "TRUE") then the calculate event macro below is triggered (with comments for guidance), The two key (bold) lines do the following:
- tests if a row value was "" but became "True", if so it:
- inserts a row 2 near the top oif sheet "Recording"
- copies A:D to that row
- adds the time to column E
- records the new values in D in the array LastVals for the next triggger of the macro.
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Dim Rw As Long
' check a change occured on the first sheet
If Sh.Name <> "Remote values" Then Exit Sub
' prevent retrigger and reverse the change
Application.EnableEvents = False
With Sheet1
LstRw = .Range("A" & Rows.Count).End(xlUp).Row
' compare to find change
For Rw = 2 To LstRw
' see if a value changed to (Boolean) True
If LastVals(Rw, 1) = "" And CStr(.Cells(Rw, 4).Value) = "True" Then
'insert row
Sheet2.Rows("2:2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
' add data and time to row 2
Sheet2.Cells(2, 1).Resize(1, 4).Value = .Cells(Rw, 1).Resize(1, 4).Value
Sheet2.Cells(2, 5).Value = Time
End If
Next Rw
'save changed values
LastVals = .Range("D1:D" & LstRw).Value
Application.EnableEvents = True
End With
End Sub
Note that it relies on you having values in column A:C of worksheet "Remote values". It's okay to use a formula (like you have in column D of that sheet).
Hope this fixes things for you.