简体   繁体   中英

Exclude Cell Range from Worksheet_Change function

I'm currently trying to set up a spread sheet which will allow me to keep a track of each time someone in the office changes any information within it, I have got this to work via the following code:-

Dim PreviousValue

 Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Value <> PreviousValue Then
    Sheets("log").Cells(65000, 1).End(xlUp).Offset(1, 0).Value = _
        Application.UserName & " changed cell " & Target.Address _
        & " from " & PreviousValue & " to " & Target.Value
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 PreviousValue = Target.Value
End Sub

Which logs any changes to all the cells in the workbook. However I also have this code:-

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
 Sheets("MEP 01").Range("D5").Value = Date
 Sheets("MEP 01").Range("E5").Value = Time
End Sub

This then logs the last time the document was saved, I would like to know if there is any way I can remove cells D5 and E5 from the audit code, as these two cells along with D4 (which contains =TODAY() formula) will change often and will make my audit trail rather large.

Any help would be most appreciated.

In your Workbook_BeforeSave add:

Application.EnableEvents = False

before changing the sheets but be sure to add

Application.EnableEvents = True

before ending the Sub.

This prevents the Worksheet_Change event from being triggered so nothing gets written to your log.

After a bit more searching I was able to locate the answer to this query:-

To exclude certain cells from the Worksheet_Change function all you have to do is add the following line of code for the cells in question:-

 If Target.Address = "Cell number" Then Exit Sub

The final code will look like this:-

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$5" Then Exit Sub
If Target.Address = "$E$5" Then Exit Sub
''Excludes cells D5 and E5 from Worksheet_Change call''
If Target.Value <> PreviousValue Then
    Sheets("log").Cells(65000, 1).End(xlUp).Offset(1, 0).Value = _
        Application.UserName & " changed cell " & Target.Address _
        & " from " & PreviousValue & " to " & Target.Value
End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
PreviousValue = Target.Value
 End Sub

I'm sure someone on here will be able to make this code look neater though as is it does what I require it to do.

You can use Intersect in your case:

 Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Value <> PreviousValue AND Intersect(Target, Range("D4,D5,E5")) Is Nothing Then
    Sheets("log").Cells(65000, 1).End(xlUp).Offset(1, 0).Value = _
        Application.UserName & " changed cell " & Target.Address _
        & " from " & PreviousValue & " to " & Target.Value
  End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM