Issue in record date and time automatically when cell changes

Gaurangg

Board Regular
Joined
Aug 6, 2015
Messages
134
Hi All,

I am not much savvy with the VBA Code building. I have googled below code and modified as my need. But when I have protected the worksheet(Unlocked the target cells) and reopen the excel, I got an error that due to security issue macro is disabled. And then it didn't capture the time.

I have made modification to capture time in two different columns when data is selected in column A and U. When I run this code in fresh column then it works and sudden after got above error & it stops. Can anyone help me with necessary changes in below code?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Update 20140722
Dim WorkRng, WorkRng1 As Range
Dim Rng, Rng1 As Range
Dim xOffsetColumn, xOffsetColumn1 As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("A:A"), Target)
xOffsetColumn = 29
If Not WorkRng Is Nothing Then
    Application.EnableEvents = False
    For Each Rng In WorkRng
        If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, xOffsetColumn).Value = Now
            Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
        Else
            Rng.Offset(0, xOffsetColumn).ClearContents
        End If
    Next
    Application.EnableEvents = True
End If
Set WorkRng1 = Intersect(Application.ActiveSheet.Range("U:U"), Target)
xOffsetColumn1 = 9
If Not WorkRng1 Is Nothing Then
    Application.EnableEvents = False
    For Each Rng1 In WorkRng1
        If Not VBA.IsEmpty(Rng1.Value) Then
            Rng1.Offset(0, xOffsetColumn1).Value = Now
            Rng1.Offset(0, xOffsetColumn1).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
        Else
            Rng1.Offset(0, xOffsetColumn1).ClearContents
        End If
    Next
    Application.EnableEvents = True
End If
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
to have your macros always work you need to change the trust centre settings in options to enable them
 
Upvote 0
After getting the error, I have checked and found that the settings are already enabled. I have also checked by adding this code to new excel and save it with settings. Then I got the error related to antivirus and then it goes disabled.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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