Write-protect cell with password VBA

KlausW

Active Member
Joined
Sep 9, 2020
Messages
378
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
 
Thanks both U, it works just as I open the file. Thank its great, have a nice day both. Many thanks fron Denmark
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Forum statistics

Threads
1,214,927
Messages
6,122,311
Members
449,080
Latest member
jmsotelo

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
Back
Top