VBA auto date / remove

cbrf23

Board Regular
Joined
Jun 20, 2011
Messages
241
I'm trying to write a macro in vba that will automatically add the date when I enter data in a certain column.

I got that part working.

Then i found, that if i deleted the entry, the date stayed.

So, I tried modifying the code to empty the date cell if I deleted the entry.... but now it doesnt work at all.

Could anyone tell me what I've done wrong here/how to fix it?

Here is what I tried:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim dd As Range, ee As Range
Set dd = Intersect(Target, Range("d:d"))
Set ee = Intersect(Target, Range("e:e"))
On Error GoTo ERRHANDLER
If (dd) Is Nothing Then
    Target.Offset(0, -3) = ""
    Else
    If Not (dd) Is Nothing Then
    Target.Offset(0, -3) = Date
    End If
End If
If (ee) Is Nothing Then
    Target.Offset(0, -3) = ""
    Else
    If Not (ee) Is Nothing Then
    Target.Offset(0, 1) = Date
    End If
End If
Exit Sub

ERRHANDLER:
Exit Sub
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Can't figure out conditions to clear/set date.
 
Upvote 0
Hi Sektor,

what i had that was working was:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ERRHANDLER
 
If Not Intersect(Target, Range("D:D")) Is Nothing Then
    Target.Offset(0, -3) = Date
End If
 
If Not Intersect(Target, Range("E:E")) Is Nothing Then
    Target.Offset(0, -3) = Date
End If
 
Exit Sub

ERRHANDLER:
Exit Sub
 
End Sub


What I find is, that when I delete an entry from column D (or E), or even if column D is blank and i hit the delete key it adds the date. Which I dont want.

Basically what I want is this: I enter data in column D and a date stamp is created 3 columns over in column A (this works as written above). But I also want to add a line that says if column D is empty, then column A also needs to be empty.

Does that make sense?
 
Upvote 0
May be this?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    If (Not Intersect(Target, Range("D:D")) Is Nothing) Or _
       (Not Intersect(Target, Range("E:E")) Is Nothing) Then
            With Target.Offset(0, -3)
                If IsEmpty(Target) Then
                    .Value = Date
                Else
                    .ClearContents
                End If
            End With
    End If
     
End Sub
 
Upvote 0
Sektor, thank you for the help!

I tried the code you posted, but it wasnt working for me. But a little tweaking, and everything is working great!

Here is what I ended up with:

Code:
    If (Not Intersect(Target, Range("D:D")) Is Nothing) Then
            If (Not IsEmpty(Target)) Then
                    Target.Offset(0, -3) = Date
                Else
                    With Target.Offset(0, -3)
                    .ClearContents
                    End With
            End If
    End If

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,224,544
Messages
6,179,430
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top