Modify code to insert date/time

RattlingCarp3048

Board Regular
Joined
Jan 12, 2022
Messages
166
Office Version
  1. 365
Platform
  1. Windows
I am working on modifying a code someone gave me on here a while back to auto enter the date/time. it has been tremendously helpful and i use it everywhere. However, this time around i am trying to mod it for a specific cell rather that an entire row or column but nothing happend when the cell changes, maybe because the value is a drop list? can someone help mod it so that if cell C29 <>"" then insert date/time in U29?

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Handler
If Target.Range("C29:H29") And Target.Value <> "" Then
Application.EnableEvents = False
Target.Range("U29") = Format(Now(), "mm-dd-yyyy hh:mm")
Application.EnableEvents = True
End If
Handler:
End Sub


1657920000730.png
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Use instead:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Handler
    If Intersect(Target, Range("C29")) And Target.Value <> "" Then '<- changed
        Application.EnableEvents = False
        Range("U29") = Format(Now(), "mm-dd-yyyy hh:mm") '<- changed
        Application.EnableEvents = True
    End If
Handler:
End Sub
 
Upvote 0
Use instead:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Handler
    If Intersect(Target, Range("C29")) And Target.Value <> "" Then '<- changed
        Application.EnableEvents = False
        Range("U29") = Format(Now(), "mm-dd-yyyy hh:mm") '<- changed
        Application.EnableEvents = True
    End If
Handler:
End Sub
nothing happened when i changed C29. i did a direct copy and paste as-is.
 
Upvote 0
Where did you paste the macro ? since it triggers by Event Worksheet_Change it must be pasted in the sheet's module.
 
Upvote 0
Where did you paste the macro ? since it triggers by Event Worksheet_Change it must be pasted in the sheet's module.
correct, thats where i pasted it. the sheet name is Master Packing List and that's the module i pasted it too

1657923055546.png


1657923347001.png
 
Upvote 0
Sorry, my mistake, copied the wrong macro, change the line of code to this:
VBA Code:
If Not Intersect(Target, Range("C29")) Is Nothing And Target.Value <> "" Then '<- changed
 
Upvote 0
Solution

Forum statistics

Threads
1,215,225
Messages
6,123,732
Members
449,116
Latest member
Aaagu

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