OSUBuckeyes
New Member
- Joined
- Aug 22, 2018
- Messages
- 4
This is my first question on this website but I've been using this site for a while now teaching myself basic VBA skills...thank you all so much!
Now to my question...
I'm looking to have a macro that will track any change on any worksheet. I've found code that does this but it only references the cell (A1 or B25) the change occurred. We have several tabs so we need to know which sheet and cell changed.
Below is the code that I am using, found on another website. What do I need to add to this to see what worksheet the change occurred? If there is a more efficient/cleaner way than below, I am open to that suggestion as well.
*Disclaimer, this is a shared file and the method to view the history of the changes via the built in function is messy, in my opinion. I'd prefer to just have a log using this macro.
Thank you in advance!
Dim vOldVal 'Must be at top of module
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim bBold As Boolean
If Target.Cells.Count > 1 Then Exit Sub
On Error Resume Next
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
If IsEmpty(vOldVal) Then vOldVal = "Empty Cell"
bBold = Target.HasFormula
With Sheets("Change Log")
If .Range("A1") = vbNullString Then
.Range("A1:E1") = Array("CELL CHANGED", "OLD VALUE", _
"NEW VALUE", "TIME OF CHANGE", "DATE OF CHANGE")
End If
With .Cells(.Rows.Count, 1).End(xlUp)(2, 1)
.Value = Target.Address
.Offset(0, 1) = vOldVal
With .Offset(0, 2)
If bBold = True Then
.ClearComments
.AddComment.Text Text:=Chr(10) & "" & Chr(10) & "Bold values are the results of formulas"
End If
.Value = Target
.Font.Bold = bBold
End With
.Offset(0, 3) = Time
.Offset(0, 4) = Date
End With
.Cells.Columns.AutoFit
End With
vOldVal = vbNullString
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
On Error GoTo 0
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
vOldVal = Target
End Sub
Now to my question...
I'm looking to have a macro that will track any change on any worksheet. I've found code that does this but it only references the cell (A1 or B25) the change occurred. We have several tabs so we need to know which sheet and cell changed.
Below is the code that I am using, found on another website. What do I need to add to this to see what worksheet the change occurred? If there is a more efficient/cleaner way than below, I am open to that suggestion as well.
*Disclaimer, this is a shared file and the method to view the history of the changes via the built in function is messy, in my opinion. I'd prefer to just have a log using this macro.
Thank you in advance!
Dim vOldVal 'Must be at top of module
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim bBold As Boolean
If Target.Cells.Count > 1 Then Exit Sub
On Error Resume Next
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
If IsEmpty(vOldVal) Then vOldVal = "Empty Cell"
bBold = Target.HasFormula
With Sheets("Change Log")
If .Range("A1") = vbNullString Then
.Range("A1:E1") = Array("CELL CHANGED", "OLD VALUE", _
"NEW VALUE", "TIME OF CHANGE", "DATE OF CHANGE")
End If
With .Cells(.Rows.Count, 1).End(xlUp)(2, 1)
.Value = Target.Address
.Offset(0, 1) = vOldVal
With .Offset(0, 2)
If bBold = True Then
.ClearComments
.AddComment.Text Text:=Chr(10) & "" & Chr(10) & "Bold values are the results of formulas"
End If
.Value = Target
.Font.Bold = bBold
End With
.Offset(0, 3) = Time
.Offset(0, 4) = Date
End With
.Cells.Columns.AutoFit
End With
vOldVal = vbNullString
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
On Error GoTo 0
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
vOldVal = Target
End Sub