I'm trying to use the following code to find some dates (VLOOKUP) on a different worksheet and fix their values in place (Copy/PasteSpecial). Then the macro should look through the dates and clear them if they are in the future (> Date).
This macro works, but the first time it's run after a new row of data is added to ws2 (happens in a macro before this one), the last row seems to be overlooked by the "For each" section of this macro. But then, if I manually run just this macro again, it will correct itself.
I've added a msg box to display the value of LrB2, and it always displays the accurate number of rows (including after a new row addition). Any thoughts?
Also, a bonus question... When this macro finishes running, it leaves the pasted area selected. What's the code to deselect, so that there isn't a dotted box around the newly pasted area?
This macro works, but the first time it's run after a new row of data is added to ws2 (happens in a macro before this one), the last row seems to be overlooked by the "For each" section of this macro. But then, if I manually run just this macro again, it will correct itself.
I've added a msg box to display the value of LrB2, and it always displays the accurate number of rows (including after a new row addition). Any thoughts?
Also, a bonus question... When this macro finishes running, it leaves the pasted area selected. What's the code to deselect, so that there isn't a dotted box around the newly pasted area?
Code:
Sub updateDates()
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Main Data")
Dim LrB2 As Long
Dim c As Range
LrB2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
MsgBox (LrB2)
On Error Resume Next
If LrB2 > 2 Then
ws2.Range("C3:C" & LrB2).Formula = "=VLOOKUP(A3, 'Main Data'!B:S,4,FALSE)"
ws2.Range("D3:D" & LrB2).Formula = "=VLOOKUP(A3, 'Main Data'!B:S,5,FALSE)"
ws2.Columns ("C:D").Copy
ws2.Range("C3").PasteSpecial xlPasteValues
Else
End If
For Each c In ws2.Range("C3:D" & LrB2)
If c > Date Then
c = ""
Else
End If
Next
End Sub