I wonder whether someone may be able to help me please.
I’m using the code below to track changes to a Excel worksheet, automatically inserting a date into column ‘A’ and the word ‘No’ into column ‘AE’ when the value of any cell within the range B5:Q2000 is changed.
Option Explicit
Public preValue As Variant
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range, res As Variant
If Target.Cells.Count > 1 Then Exit Sub
On Error Resume Next
If Not Intersect(Target, Range("B5:Q2000")) Is Nothing Then
If Target.Value <> preValue And Target.Value <> "" Then
Application.EnableEvents = False
Range("A5:A" & Target.Row).Value = Date
Range("AE5:AE" & Target.Row).Value = "No"
Application.EnableEvents = True
Target.ClearComments
' Target.AddComment.Text Text:="Previous Value was " & preValue & Chr(10) & "Revised " & Format(Date, "dd-mm-yyyy") & Chr(10) & "By " & Environ("UserName")
Target.Interior.ColorIndex = 35
End If
End If
On Error GoTo 0
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count = 1 Then preValue = Target.Value
End Sub
Unfortunately I’ve come across a problem which I’m not sure how to resolve.
If the user inserts data into cells D5 and D10, the date and the word ‘No’ are added to the correct columns (A and AE) for these two rows.
However, unfortunately, the ‘No’ value and the date are also added to the these columns for the rows in between i.e D6-D9, even though the user hasn’t entered any other data on these rows, and I’m not sure where the problem lies.
I just wondered whether someone may be able to take a look at this and offer some guidance on how I may go about solving this.
Many thanks and kind regards
Your current code is applying the changes starting with
A5andAE5throughTarget'srow. To have the code change only the rows of the selected cell, change this:to this:
Also, you don’t seem to be using the variable
Cellorres. You might want to delete that line of code.