Lock cell after first edit, tracking date/time/user in shared workbook

Gusung

New Member
Joined
Feb 9, 2017
Messages
2
Hi

I have problems with writing a VBA for a shared workbook. I want to set up a shared workbook with the following function.
1) allow users to edit column D, Column A-C tracks the edit Date, time, username
2) Lock cells in column D after the first edit
3) Lock cells Column A-C to prevent users from changing it

The below is the working VBA code for tracking the date/time/username based on data keyed into column D.
But since protect and deprotect doesn't work on a shared workbook. I just use the normal "Protect Sheet" function and put it in a local shared folder instead of using the "Share Workbook" function. There's a problem: if a user doesn't hit the "Enable Content" button when they first open the workbook, the code doesn't work - meaning they can type in column D but Column A-C doesn't record the Date, time and username. Need some help with this. Thanks

Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, B As Range, Inte As Range, r As Range
Set A = Range("D:D")
Set Inte = Intersect(A, Target)
If Inte Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In Inte
If r.Value > 0 Then
ActiveSheet.Unprotect
ActiveSheet.Unprotect
r.Offset(0, -3).Value = Date
r.Offset(0, -3).NumberFormat = "dd-mm-yyyy"
r.Offset(0, -2).Value = Time
r.Offset(0, -2).NumberFormat = "hh:mm:ss AM/PM"
r.Offset(0, -1).Value = Application.UserName
Target.Locked = True
ActiveSheet.Protect
Else
End If
Next r
Application.EnableEvents = True
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How do I set two simultaneous code to run?

The below macros is for Locking column E after first edit but I wish to do the same for Column F and G as well but without tracking the Username, date and time. Thanks.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, B As Range, Inte As Range, r As Range
Set A = Range("E:E")
Set Inte = Intersect(A, Target)
If Inte Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In Inte
If r.Value > 0 Then
ActiveSheet.Unprotect Password:="123"
r.Offset(0, -4).Value = Date
r.Offset(0, -4).NumberFormat = "dd-mm-yyyy"
r.Offset(0, -3).Value = Time
r.Offset(0, -3).NumberFormat = "hh:mm:ss AM/PM"
r.Offset(0, -2).Value = Application.UserName
Target.Locked = True
ActiveSheet.Protect Password:="123"
Else
End If
Next r
Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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