I hope someone can help, my VBA is some what limited, its been a long time.
The code I have put together is working ok apart from a few small problems.
Im hoping someone can look at my code and tell me where i'm going wrong.
If I explain what it is ment to do first, then this should make things easier.
Basically, when a user inputs any text in to a cell the cell automatically creates a comment of when, what and who by, and then locks the cell preventing further editing. This all works fine. So whats the problem? Users have started using copy and paste when multiple entries are required. If a single protected cell is copied to another single cell then the code still works fine, but if a single cell is copied and pasted into multiple cells then the value is copied to the cells, but the multiple cells do not lock? If the value in these cells is edited further then they lock.
So basically I need the code to work when multiple cells have been pasted, rather than just one cell being edited.
Hope someone is able to help?
Here is the code:-
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("A4:A1000,B4:B1000,C4:C1000,D4:D1000,E4:E1000,F4:F1000,G4:G1000,H4:H1000,I4:I1000,J4:J1000,K4:K1000")) Is Nothing Then Exit Sub
If Target.Value <> "" Then
ActiveSheet.Unprotect Password:="AA"
Dim NewText As String
Dim NewVal As Variant
Dim OldText As String
Dim OldVal As Variant
Application.EnableEvents = False
NewVal = Target.Value
Application.Undo
OldVal = ActiveCell.Value
ActiveCell = NewVal
Application.EnableEvents = True
NewText = "On " & Now() & " cell changed from " & OldVal _
& " to " & NewVal & " by " & Environ("UserName")
If ActiveCell.Comment Is Nothing Then
ActiveCell.AddComment
End If
With ActiveCell.Comment
.Shape.TextFrame.AutoSize = True
OldText = .Text & vbLf
.Text Text:=OldText & NewText
End With
Target.Locked = True
ActiveSheet.Protect Password:="AA"
End If
End Sub
Bookmarks