I am trying to get the target to delete its contents if the selected cell is blank. When I try to delete the contents of the cell within the selected range, excel crashes? I tried adding a cal_loop but I'm not sure if it is helping, not sure how to use it properly... please help!
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Range("J:J,N:N,Q:Q,U:U,Z:Z,AD:AD,AG:AG")) Is Nothing Then
If Target.Value <> "" Then
Debug.Print "Change Detected 1"
Print
With Target(1, 2)
.Value = Date
End With
Else
If Target.Value = "" Then
Debug.Print "Change Detected 2"
With Target(1, 2)
.Value = ""
End If
End If
End If
Application.EnableEvents = True
End Sub
Expecting the contents of target cell to be cleared when I remove the contents of the cell within the selected range.
Edit 1:
Thanks guys! I added Application.EnableEvents = False/True at the beginning/end of the code respectively and removed the cal_loop and that stopped the crashing but now it is like the script is not running at all? Following the suggestions of another article I tried adding a Debug.print to see if I get anything (first time I am using this).. but again nothing happens. Why is that?
Edit 2:
Thanks for the enable events check, that got it working again. I added some comments as suggested to better explain what should be happening. Where it should 'Clear data if target is empty' is not working.
When I delete the contents of the target I expect the offset target (1,2) to be cleared as well.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Range("J:J,N:N,Q:Q,U:U,Z:Z,AD:AD,AG:AG")) Is Nothing Then
If Target.Value <> "" Then 'Insert date if target is not empty'
With Target(1, 2)
.Value = Date
End With
End If
Else 'Clear date if target is empty'
If Target.Value = "" Then
With Target(1, 2)
.Value = ""
End With
End If
End If
Application.EnableEvents = True
End Sub