I have a problem in audit trail with VBA

salahianoo

New Member
Joined
Feb 21, 2024
Messages
4
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I have a workbook in read only mode and im the admin so the other users can edit in cells i decided only and when they save their changes they are saving it in new work book (system required unfortunately)
now i have to make audit trail on the basic work book
i used this code but it is not working in read only mode
please help me as u can

=================================
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
Dim ov As Variant, nv As Variant
nv = Target.Value
Application.Undo
ov = Target.Value
Target = nv
With Sheets("Log")
On Error GoTo HDL:
.Cells(Rows.Count, 1).End(xlUp)(2) = Environ("UserName")
.Cells(Rows.Count, 1).End(xlUp).Offset(, 1) = Target.Parent.Name
.Cells(Rows.Count, 1).End(xlUp).Offset(, 2) = Target.Address
If ov = "" Then
.Cells(Rows.Count, 1).End(xlUp).Offset(, 3) = "Blank"
Else
.Cells(Rows.Count, 1).End(xlUp).Offset(, 3) = ov
End If
If nv = "" Then
.Cells(Rows.Count, 1).End(xlUp).Offset(, 4) = "Blank"
Else
.Cells(Rows.Count, 1).End(xlUp).Offset(, 4) = nv
End If
.Cells(Rows.Count, 1).End(xlUp).Offset(, 5) = Now
If .Columns(6).ColumnWidth < 15 Then .Columns(6).ColumnWidth = 15
End With
HDL:
If Err.Number > 0 Then
MsgBox "The following error occurred:" & Err.Number & vbLf & Err.Description & "."
End If
Application.EnableEvents = True
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Well, if it is in read only mode, you are not going to be able to make changes to it.
Better off to use Worksheet protect and protect the sheet.
Then, have your VBA code unprotect the the worksheet at the beginning of the code, make your changes, then re-protect the worksheet at the end of the code.

See here for a simple example: Allow VBA to modify cells in protected sheet
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,205
Members
452,618
Latest member
Tam84

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