Vba speed up my code!

DaveR

Board Regular
Joined
May 10, 2006
Messages
176
I have the following vba which simply tracks what users of the database do (For auditing reasons)

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal target As Range)
Dim LR As Long
If Sh.Name = "Variations" Then Exit Sub
Application.EnableEvents = False

 Application.ScreenUpdating = False 'turn off screen updating to speed things up

With Sheets("Variations")
    'Unprotect sheet
    .Unprotect "xxx"
    
     LR = .Range("A" & Rows.Count).End(xlUp).Row
    .Range("A" & LR + 1).Value = Now
    .Range("B" & LR + 1).Value = Sh.Name
    .Range("C" & LR + 1).NumberFormat = "@"
    .Range("C" & LR + 1).Value = target.Address(False, False)
    .Range("D" & LR + 1).Value = target.Value
    .Range("E" & LR + 1).Value = Environ("username")

    
    'Protect the sheet again
    .Protect "xxx"
    
     Application.ScreenUpdating = True 'turn on screen updating

End With
Application.EnableEvents = True
End Sub

However, if I add a new line to a worksheet (quite a common event!), the routine takes an age to process this.

Is there a quicker way to do this?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Not sure I'm getting this right, or not -- but you have the code line:

If Sh.Name = "Variations" Then Exit Sub

3 code lines later you have the code line:

With Sheets("Variations")
.............
End With

Seems like the 1st Statement - EXITED YOU from the Macro - so Excel is Never going to see the 2nd Code line(s)......

Confused...
 
Upvote 0
Hello

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal target As Range)
    Dim LR As Long
    If Sh.Name = "Variations" Then Exit Sub
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    With Sheets("Variations")
        'Unprotect sheet
        '.Unprotect "xxx"
        LR = .Cells(Rows.Count, 1).End(xlUp).Row + 1
        .Cells(LR, 1) = Now
        .Cells(LR, 2) = Sh.Name
        .Cells(LR, 3).NumberFormat = "@"
        .Cells(LR, 3) = target.Address(False, False)
        .Cells(LR, 4) = target.Value
        .Cells(LR, 5) = Environ("username")
        'Protect the sheet again
        '.Protect "xxx"
    End With
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
End Sub
Try to hide the paper 'Variations' with
Alt F11
F4
Visible

Excel 2003
 
Upvote 0
By "add a new line" do you mean if you insert a row?

@Jim - Variations is the sheet on which the changes are logged. The second line exits the procedure if something on that sheet is changed. Changes on all other sheets are logged.
 
Upvote 0
Yes, I mean adding in a new row.

For the purposes of the auditing, I don't need to record this really as when the data is entered, this will be added anyway.
 
Upvote 0
Try:

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim LR As Long
    If Sh.Name = "Variations" Then Exit Sub
    If Target.Address = Target.EntireRow.Address Then Exit Sub
    Application.EnableEvents = False
    Application.ScreenUpdating = False 'turn off screen updating to speed things up
    With Sheets("Variations")
'       Unprotect sheet
        .Unprotect "xxx"
         LR = .Range("A" & Rows.Count).End(xlUp).Row
        .Range("A" & LR + 1).Value = Now
        .Range("B" & LR + 1).Value = Sh.Name
        .Range("C" & LR + 1).NumberFormat = "@"
        .Range("C" & LR + 1).Value = Target.Address(False, False)
        .Range("D" & LR + 1).Value = Target.Value
        .Range("E" & LR + 1).Value = Environ("username")
'       Protect the sheet again
        .Protect "xxx"
        Application.ScreenUpdating = True 'turn on screen updating
    End With
    Application.EnableEvents = True
End Sub
 
Upvote 0
Just tried it, still taking 40 seconds to loop through to completion.

Perhaps it would be quicker simply to trap the new row and not record it?
 
Upvote 0
I'm not sure it is doing it correctly as the 'Variations' sheet still shows that 63 changes have been made......
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,917
Members
449,093
Latest member
dbomb1414

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