Write-protect cell with password VBA

KlausW

Board Regular
Joined
Sep 9, 2020
Messages
183
Office Version
  1. 2016
Platform
  1. 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


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
 

KlausW

Board Regular
Joined
Sep 9, 2020
Messages
183
Office Version
  1. 2016
Platform
  1. Windows
Thanks both U, it works just as I open the file. Thank its great, have a nice day both. Many thanks fron Denmark
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,482
Messages
5,770,349
Members
425,612
Latest member
martinijr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top