Home > Mobile >  Inserting Auto-Date Change back to blank code?
Inserting Auto-Date Change back to blank code?

Time:07-04

I am relatively new to VBA and found this code string online.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("A1:A1000")) Is Nothing Then
        With Target(1, 2)
        .Value = Date
        .EntireColumn.AutoFit
        End With
    End If
End Sub

Essentially, what it does is if a value is placed into col A. Then today's date is put into col B. However, if I were to then remove the value from col A the date still stays in col B. Is it possible to write the script to where the date in col B is removed if col A is cleared out of any values?

CodePudding user response:

Try below sub-

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("A1:A1000")) Is Nothing Then
    '        With Target(1, 2)
    '        .Value = Date
    '        .EntireColumn.AutoFit
    '        End With
    
        If Target = "" Then
            Target.Offset(, 1) = ""
        Else
            Target.Offset(, 1) = Date
            Target.EntireColumn.AutoFit
        End If
    End If
End Sub
  • Related