PDA

View Full Version : Do not update range if cell is not empty



JILBO
09-09-2021, 10:36 AM
Howdi,

Have this code to Add the date and time into column C when the 'Change Number' cell is changed.

However i don't want this to update if there is already a date in column C...im stumped!

Hope you can help



Private Sub Worksheet_Change(ByVal Target As Range)Dim r As Range, Intersection As Range, cell As Range
Set r = Range("ChgTBL[Change Number]")
Set Intersection = Intersect(r, Target)


If Intersection Is Nothing Then Exit Sub


Application.EnableEvents = False
For Each cell In Intersection
Range("C" & cell.Row).Value = Date + Time
Range("D" & cell.Row).Value = Application.UserName
Next cell
Application.EnableEvents = True




End Sub

arnelgp
09-09-2021, 07:55 PM
Private Sub Worksheet_Change(ByVal Target As Range)Dim r As Range, Intersection As Range, cell As Range
Set r = Range("ChgTBL[Change Number]")
Set Intersection = Intersect(r, Target)




If Intersection Is Nothing Then Exit Sub




Application.EnableEvents = False
For Each cell In Intersection
If Len(Range("C" & cell.Row) & "") = 0 Then
Range("C" & cell.Row).Value = Date + Time
End If
If Len(Range("D" & cell.Row) & "") = 0 Then
Range("D" & cell.Row).Value = Application.UserName
End If
Next cell
Application.EnableEvents = True
End Sub

JILBO
09-10-2021, 12:35 AM
Thankyou Arnelgp! that works perfectly!!

Just another thing i've noticed is it records a change event when the table is expanded or when you just click in the Change Number column. Is there a way to not run the event until a value greater than 0 is added?

arnelgp
09-10-2021, 01:12 AM
the event wil Fire whenever you change any cell in the worksheet
regardless if it is numeric or string that you add/edit .

JILBO
09-10-2021, 03:51 AM
Ah ok, thanks for support and responses. Its working great