I am a first time poster so please be gentle. I have some code I found somewhere that auto populates the date in column C when the neighboring cell in column B is not null. The code is working fine for new entries but when the neighboring cell in column B is changed a week or so later it overwrites column C with the current date. This is a check ledger so the original date must remain. The code I'm using is below and you can see a commented line that I thought would exit the sub if the cell in C was not nothing but it doesn't work. Of course I can just type over the date to correct it but if another user isn't paying attention to the date first before VOIDing a check it gets overwritten. In the example sheet below you don't know which date the VOIDed line should have since the date above and below are different.
'Auto add date to cell when a specific other cell is not null
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
'If only one cell at a time should be changed, un-comment the next line
If Target.Cells.Count > 1 Then Exit Sub
'To allow deletions to be bypassed without running the code, un-comment the next line
If Target.Cells(1).Value = "" Then Exit Sub
If Intersect(Target, Range("B:B")) Is Nothing Then Exit Sub 'specific range
'If Not Intersect(Target, Range("C:C")) Is Nothing Then Exit Sub 'specific range ' I tried this to no avail
'Turn off events to keep out of loops
Application.EnableEvents = False
For Each c In Intersect(Target, Range("B:B"))
If c.Value <> "" Then
Cells(c.Row, "C").NumberFormat = "m/d/yyyy"
Cells(c.Row, "C").Value = Date
End If
Next c
Application.EnableEvents = True
End Sub
'Auto add date to cell when a specific other cell is not null
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
'If only one cell at a time should be changed, un-comment the next line
If Target.Cells.Count > 1 Then Exit Sub
'To allow deletions to be bypassed without running the code, un-comment the next line
If Target.Cells(1).Value = "" Then Exit Sub
If Intersect(Target, Range("B:B")) Is Nothing Then Exit Sub 'specific range
'If Not Intersect(Target, Range("C:C")) Is Nothing Then Exit Sub 'specific range ' I tried this to no avail
'Turn off events to keep out of loops
Application.EnableEvents = False
For Each c In Intersect(Target, Range("B:B"))
If c.Value <> "" Then
Cells(c.Row, "C").NumberFormat = "m/d/yyyy"
Cells(c.Row, "C").Value = Date
End If
Next c
Application.EnableEvents = True
End Sub
Check # | Payee | Date |
11923 | Bob's | 8/15/2024 |
11924 | Gary's | 8/15/2024 |
11925 | Jim's | 8/15/2024 |
11926 | Kim's | 8/15/2024 |
11927 | Steve's | 8/15/2024 |
11928 | Gomez's | 8/16/2024 |
11929 | Freeman's | 8/16/2024 |
11930 | Alpha's | 8/19/2024 |
11931 | Nathan's | 8/19/2024 |
11932 | John's | 8/19/2024 |
11933 | Anna's | 8/19/2024 |
11934 | Lilly's | 8/19/2024 |
11935 | VOID | 9/13/2024 |
11936 | Norm's | 8/20/2024 |