Excel date stamp

johnlad

New Member
Joined
Feb 1, 2011
Messages
12
Hi everyone, I was hoping to get some help with a spreadsheet I am creating for work. I have had this partly answered on here but theres a slight issue with it that I hope someone knows the solution to.

I have a spreadsheet with a "Yes" "No" drop down boxes set up in column G. These run down the column to row 16. What I want is for the date that the drop down box is changed to "Yes" to be entered in the cell to the right of that box. So, if the drop down box in cell G2 is changed to "Yes" today (21/02/11) the cell to the right (H2) will enter the date of when that work was completed. I have the code for this working and set up in VBA. It looks like this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 And LCase(Target.Value) = "yes" Then
    Application.EnableEvents = False
    Target.Offset(, 1).Value = Date
    Application.EnableEvents = True
End If
End Sub

The only problem is when I try to change the "Yes" back to "No". When I do this the date stays as it was in the cell next to it. So if a mistake is made and the wrong cell is changed back to "No" the date remains. Does anyone know how I can make the cell to the right of the drop down blank again if it is changed back?

Any help with this will be greatly appreciated. :)
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 Then
    Application.EnableEvents = False
    Target.Offset(, 1).Value = IIf(LCase(Target.Value) = "yes", Date, "")
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Yeah that vba one works thank you for your help. I really appreciate it.

The reason I'm not using cells is because other people who aren't that computer literate will be updating it so I want the code tucked away to avoid them from accidently editing/deleting the code. Thanks for your suggestion though. :)
 
Upvote 0
Just noticed one last thing. I have multiple columns and have tried adding the code again for another column and changing the column to look at number 10. But because the code is a duplicate it is coming up with errrors. I realise this is because of the "Worksheet_Change" sub being a duplicate. How do I make multiple columns of this code? thanks
 
Upvote 0
You need to combine them:

One way

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 Then
    Application.EnableEvents = False
    Target.Offset(, 1).Value = IIf(LCase(Target.Value) = "yes", Date, "")
    Application.EnableEvents = True
ElseIf Target.Column = 10 Then
    Application.EnableEvents = False
    Target.Offset(, 1).Value = IIf(LCase(Target.Value) = "yes", Date, "")
    Application.EnableEvents = True
End If
End Sub

But if you are doing the same thing with both columns then this is neater

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 Or Target.Column = 10 Then
    Application.EnableEvents = False
    Target.Offset(, 1).Value = IIf(LCase(Target.Value) = "yes", Date, "")
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Thank you for help again. That works. Been trying learn VBA after starting this spreadsheet just didn't realise how much there was to it

Thank you
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,507
Members
452,917
Latest member
MrsMSalt

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