Same VBA code in the same worksheet for 2 different sets of table the first works, the second doesnt

118 views Asked by At

I found the below code to input a timestamp automatically when one specific range has anything written and I wanted to do this in 2 specific places within the same worksheet, hence I wrote the same sequence, however the 1st works and the second doesnt, below my code:

Private Sub Worksheet_Change(ByVal Target As Range)
'==============logdate timestamp completed============
Dim myTableRange As Range
Dim myDayTimeRange As Range


'my Data Table Range
Set myTableRange = Range("E:E")

If Not Intersect(Target, myTableRange) Is Nothing Then

'Column for the date
Set myDateTimeRange = Range("A" & Target.Row)

If myDateTimeRange.Value = "" Then

    myDateTimeRange.Value = Now
End If

End If


'==============logdate timestamp inflight============
Dim myTableRangeif As Range
Dim myDayTimeRangeif As Range

'my Data Table Range
Set myTableRangeif = Range("N19:R19")

If Not Intersect(Target, myTableRangeif) Is Nothing Then

'Column for the date
Set myDateTimeRangeif = Range("J" & Target.Row)

If myDateTimeRangeif.Value = "" Then

    myDateTimeRangeif.Value = Now
End If
End If

End Sub

Just changed the code as suggested however the timestamp appears on the "A" column but not on the "J" one

1

There are 1 answers

6
BigBen On BEST ANSWER

When you change something in Range("N19:R19"), the Exit Sub is executed because it is true that Intersect(Target, myTableRange) Is Nothing.

You need to change the logic of Exiting the Sub if the Intersection is nothing.

If Not Intersect(Target, myTableRange) Is Nothing Then
    'Column for the date
    Set myDateTimeRange = Range("A" & Target.Row)

    If myDateTimeRange.Value = "" Then
        myDateTimeRange.Value = Now
    End If

End If

...

If Not Intersect(Target, myTableRangeif) Is Nothing Then
    'Column for the date
    Set myDateTimeRangeif = Range("J" & Target.Row)

    If myDateTimeRangeif.Value = "" Then
        myDateTimeRangeif.Value = Now
    End If

End If