VBA - Track Changes in Separate Worksheet

sektr

New Member
Joined
Mar 22, 2019
Messages
1
I am trying to set up code so that when I make changes to a cell in a particular worksheet, named "Data", the changes are logged in my "Log" worksheet in a table with the following columns:

1. Worksheet Name
2. Item Number (the data from the cell in the corresponding row in column A of the Data sheet)
3. Column Name (text string from row 1 of the Data sheet)
4. Old Value
5. New Value
6. Time

I found some sample code online that I thought would work, but not sure if I'm on the right track in adapting it. Here is what I have so far, but I'm not sure how to define the other variables or if this structure will even work:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


'Dimensions
Dim SerialNumber As Single
Dim ColumnName As String
Dim oldValue As Double
Dim newValue As Double
Dim dtmTime As Date


'Paramaters
dtmTime = Now
newValue = Target.Value




Dim WorkSheetName As String
    WorkSheetName = "Data"



'If ActiveSheet.Name <> "Data" Then
'    Application.EnableEvents = False




Sheets("Log").Range("A" & rows.Count).End(xlUp).Offset(1, 0).Value = ActiveSheet.Name
Sheets("Log").Range("A" & rows.Count).End(xlUp).Offset(0, 1).Value = SerialNumber
Sheets("Log").Range("A" & rows.Count).End(xlUp).Offset(0, 2).Value = ColumnName
Sheets("Log").Range("A" & rows.Count).End(xlUp).Offset(0, 3).Value = oldValue
Sheets("Log").Range("A" & rows.Count).End(xlUp).Offset(0, 4).Value = newValue
Sheets("Log").Range("A" & rows.Count).End(xlUp).Offset(0, 5).Value = dtmTime




Sheets("Log").Columns("A:F").AutoFit
'    Application.EnableEvents = True
'End If
    
End Sub

I have the ending If statement disabled right now as when it is enabled the macro does not run, although it was included in the code I sampled this from.

Super new to VBA so any help is really appreciated!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
.
The edited version functions but it is not capturing the SERIAL NUMBER, COLUMN NAME, or OLD VALUE.

The macro is missing the code to capture the above. Re-check the resource website.

Code:
Option Explicit


Private Sub Worksheet_Change(ByVal Target As Range)




'Dimensions
Dim SerialNumber As Single
Dim ColumnName As String
Dim oldValue As Double
Dim newValue As String  '<----- changed to STRING.
Dim dtmTime As Date


'Paramaters
dtmTime = Now
newValue = Target.Value


Dim WorkSheetName As String
    WorkSheetName = "Data"


'If ActiveSheet.Name <> "Data" Then
'    Application.EnableEvents = False


Sheets("Log").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = ActiveSheet.Name
Sheets("Log").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = SerialNumber
Sheets("Log").Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = ColumnName
Sheets("Log").Range("A" & Rows.Count).End(xlUp).Offset(0, 3).Value = oldValue
Sheets("Log").Range("A" & Rows.Count).End(xlUp).Offset(0, 4).Value = newValue
Sheets("Log").Range("A" & Rows.Count).End(xlUp).Offset(0, 5).Value = dtmTime


Sheets("Log").Columns("A:F").AutoFit
'    Application.EnableEvents = True
'End If
    
End Sub
 
Upvote 0
.
Or if you are interested ... I can provide a completed project using another macro.
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,209
Members
448,874
Latest member
b1step2far

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