Need Help on Tracking Changes in Excel (Data logger-VBA)

chobeo31

New Member
Joined
Dec 22, 2014
Messages
14
I want to track any modify or change in my set a given of data. After quite a time figuring on my own, nothing works as I expected.

The data set is ranged between A1:G6 on "Sheet1". I assume that any new entry would follow the order.

A
B
C
D
E
F
G
1
Order ID
Client
Time
Type
Product
Quantity
Price
2
1234
aa
2014/12/23 12:00
buy
ABC
2
3250
3
1235
bb
2014/12/24 12:00
sell
XYZ
1
3221
4
1236
cc
2014/12/21 12:00
buy
XYZ
5
3260
5
1237
dd
2014/12/23 12:00
sell
ABC
7
3150

<tbody>
</tbody>

Now I want to track any modified or changed affect the data set and view that on "Sheet2".
For example, if F2 changed into 6 and G5 changed into 3155, respectively, on "Sheet2", 2 entries recorded like this

A
B
C
D
E
1
Date and Time
Order ID
Comment
Old
New
2
2014/12/23 12:15
1234
Quantity modified
2
6
3
2014/12/23 12:17
1237
Price modified
3150
3155

<tbody>
</tbody>

Any suggestion?
Please help a newbie in VBA.
Thanks a lot.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
.
.

This macro should do the job, but you must place it in the code module corresponding to Sheet1 (otherwise it won't work):

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

  Dim rngTrackRange As Range
  Dim varHeaders    As Variant
  Dim rngSelection  As Range
  Dim rngTrackCell  As Range
  Dim varOldVal     As Variant
  Dim lngLogRow     As Long
  Dim intColIndex   As Integer
  
  With Me
    On Error Resume Next
    Set rngTrackRange = Intersect(Range(.Cells(2, 1), .Cells(6, 7)), Target)
    On Error GoTo 0
    If rngTrackRange Is Nothing Then Exit Sub
    varHeaders = Range(.Cells(1, 1), .Cells(1, 7)).Value
  End With
  Set rngSelection = Selection
  
  Application.EnableEvents = False
  With ThisWorkbook.Worksheets(2)
    For Each rngTrackCell In rngTrackRange
      Application.Undo
      varOldVal = rngTrackCell.Value
      Application.Undo
      lngLogRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
      intColIndex = rngTrackCell.Column
      .Cells(lngLogRow, 1).Value = Now
      .Cells(lngLogRow, 2).Value = Me.Cells(rngTrackCell.Row, 1).Value
      .Cells(lngLogRow, 3).Value = varHeaders(1, intColIndex) & " Modified"
      .Cells(lngLogRow, 4).Value = varOldVal
      .Cells(lngLogRow, 5).Value = rngTrackCell.Value
    Next rngTrackCell
  End With
  Application.EnableEvents = True
  rngSelection.Select

End Sub
 
Upvote 0
Oh my dear, you are such a saint in this holiday you know. So many grateful.
It works like a miracle.
However, I dont know where the prob is that it worked only a few times before stopping. (May be within 1 min or before autosave, I guess) Do you have the same problem?
 
Upvote 0
Ahha, I got something. Because of the range (your default is only within cell(6,7)) when I modify beside that range, no log would be recorded; now I fixed it a bit and retest. Till now, it works perfectly just the way I expect. I will tell you the result later.
Many thanks.
 
Upvote 0

Forum statistics

Threads
1,216,106
Messages
6,128,863
Members
449,473
Latest member
soumyahalder4

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