A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
also just remember that i need to also do
With Target(1).Offset(0, -2)
Sheet3.Range("a1").NumberFormat = .NumberFormat
Sheet3.Range("a1").Value2 = .Value2
Use VBA help to learn about the structure of the With statement.
See the complete VBA procedure below. Alternatively, download "new+old Wrksht-Chng.xlsm" (click here). Ignore any preview errors and request to login. Just proceed to download the file.
BTW, I don't see how you expect anyone to edit C1:C2 -- or A2:A50, your original range -- when the worksheet is protected by a password. No need to explain to me. Just bringing it to your attention.
In the code below, pay close attention to other changes, highlighted in red.
Private Sub Worksheet_Change(ByVal Target As Range)
Const mypass As String = "password" '*** new ***
' *** new code
If Not Intersect(Target, Range("c1:c3")) Is Nothing Then
Application.EnableEvents = False
Me.Unprotect Password:=mypass
With Target(1).Offset(0, -2)
Sheet3.Range("a1").NumberFormat = .NumberFormat
Sheet3.Range("a1").Value2 = .Value2
End With
With Target(1).Offset(0, 3)
Sheet3.Range("a2").NumberFormat = .NumberFormat
Sheet3.Range("a2").Value2 = .Value2
End With
Me.Protect Password:=mypass
Application.EnableEvents = True
Exit Sub
End If
' *** old code
Dim mytable As Range
Set mytable = Range("A2", "A50")
Dim Tr As Range
Set Tr = Application.Intersect(Target, mytable)
If Tr Is Nothing Then Exit Sub
Application.EnableEvents = False
Me.Unprotect Password:=mypass '*** modified ***
If Tr.Value = "" Then
Tr.Offset(0, 3).ClearContents
ElseIf IsNumeric(Tr.Value) Then
Tr.Offset(0, 3).Value = Now()
Target.Formula = "=VLOOKUP(" & Target.Value & ",login!A1:B70,2,FALSE)"
End If
Me.Protect Password:=mypass '*** modified ***
Application.EnableEvents = True
End Sub