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
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
The Workbook_Open event doesn't have a Target argument.

What do you mean by protecting cell A8? Typically worksheets are protected not cells.
One quick and dirty way to protect only Cell A8 would be

VBA Code:
With ActiveSheet
    .Unprotect
    .Cells.Locked = False
    .Range("A8").Locked = True
    .Protect
End With
 
Upvote 0
The Workbook_Open event doesn't have a Target argument.

What do you mean by protecting cell A8? Typically worksheets are protected not cells.
One quick and dirty way to protect only Cell A8 would be

VBA Code:
With ActiveSheet
    .Unprotect
    .Cells.Locked = False
    .Range("A8").Locked = True
    .Protect
End With
Thanks, do I have to put the code into a module? KW
 
Upvote 0
Thanks, do I have to put the code into a module? KW
I would like that if you try to type something in cell a8, there should be a box with the text “enter password”, if you type the correct password you can type in the cell, if not you should be asked to type password again.

KW
 
Upvote 0
You could put this in the code module for the sheet in question
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Target, Me.Range("A8")) Is Nothing Then
        If Not IsPassword Then
            Application.EnableEvents = False
            Application.Undo
            Application.EnableEvents = True
            Beep
        End If
    End If
End Sub

Private Function IsPassword() As Boolean
    Const myPassword As String = "password"
    IsPassword = (Application.InputBox("enter password", Default:=myPassword, Type:=2) = myPassword)
    If Not IsPassword Then
        IsPassword = (Application.InputBox("Try again", Default:=myPassword, Type:=2) = myPassword)
    End If
End Function
 
Upvote 0
I would like that if you try to type something in cell a8, there should be a box with the text “enter password”, if you type the correct password you can type in the cell, if not you should be asked to type password again.
Hi, so according to the previous just paste this worksheet event code to the worksheet module :​
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 Static Passed As Boolean
    Dim P$
        If Target.Address <> "$A$8" Or Passed Then Exit Sub
    Do
        P = InputBox(vbLf & vbLf & " Enter password to modify this cell :", "Limited cell access")
        If P = "" Then Target(1, 2).Select: Exit Sub
    Loop Until P = "Password"
        Passed = True
End Sub
 
Upvote 0
Solution
Thanks both, I simply do not know what I am doing wrong I put the VBA code into a module. Saves the file with marco, opens the file again. But I can still write in cell A8 in. Could it be because there are two sheets, cell A8 is in the "Prisliste" sheet. KW
 
Upvote 0
Klaus, as we both wrote the code must not be « into a module » but within the Prisliste worksheet module …​
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,352
Members
449,080
Latest member
Armadillos

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