KlausW
Active Member
- Joined
- Sep 9, 2020
- Messages
- 378
- Office Version
- 2016
- Platform
- Windows
Hi
I use this code to protect cell A8 with a password, but it does not work, the vba code must start when the file is opened.
It is located in a VBA module.
Someone who can help.
All help will be appreciated.
Best regards Klaus W
I use this code to protect cell A8 with a password, but it does not work, the vba code must start when the file is opened.
It is located in a VBA module.
Someone who can help.
All help will be appreciated.
Best regards Klaus W
VBA Code:
Private Sub Worksheet_Open(ByVal Target As Range)
Dim SAdress As String
Dim vOLD As Variant
Dim aNEW As Variant
Dim vNEW As Variant
Dim myValue As Variant
SAdress = Target.Address(False, False)
If SAdress = "a8" Then
vNEW = Target.Value
aNEW = Target.Address
Application.EnableEvents = False
Application.Undo
vOLD = Target.Value
Target.Value = vNEW
If Not vOLD = vNEW Then
myValue = InputBox("Enter the code")
If myValue = 12345 Then
Application.ActiveSheet.Name = VBA.Left(Target, 31)
Else:
MsgBox " The entered code is wrong "
Ark1.Range(Target.Address).Value = vOLD
Exit Sub
End If
If Not myValue <> "" Then
Ark1.Range(Target.Address).Value = vOLD
End If
End If
Application.EnableEvents = True
End If
End Sub