Password protect column with unlock button

newtoexel79

New Member
Joined
Nov 14, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

I am trying to create a simple VBA code so that a specific column in a worksheet is unlocked when a button is presses and the password is inputted. Then I would like it to lock the column again after saving. I am really struggling to get it to work right. Basically want the column shown to be locked until the "AI Approval" button is clicked, then after they enter the password they can input information into the column below. Once they hit save it should relock the column. Thank you in advance!!

1699973666683.png
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi @newtoexel79. Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Follow the instructions carefully!

1. Put the following code in your button.​
2. Change CommandButton1 to the name of your button.​
3. Change Sheet1 to the name of your sheet.​
4. Change abc for the password of your sheet.​
5. Change xyz by password to allow modifying the column.​

Rich (BB code):
Private Sub CommandButton1_Click()
  Dim pwd As Variant
 
  pwd = InputBox("Please Enter password", "EDIT AI COLUMN")
 
  If StrPtr(pwd) = 0 Then
    MsgBox "Cancelled"
  ElseIf Len(pwd) = 0 Then
    MsgBox "Please Enter a valid value"
  Else
    If pwd = "xyz" Then
      With Sheets("Sheet1")
        .Unprotect "abc"
        .Range("AI:AI").Locked = False
        .Protect "abc"
      End With
      MsgBox "Now you can edit the AI column"
    End If
  End If
End Sub

6. Put the following code in Thisworkbook events.​
7. Change Sheet1 to the name of your sheet.​
8. Change abc for the password of your sheet.​

Rich (BB code):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  With Sheets("Sheet1")
    .Unprotect "abc"
    .Range("AI:AI").Locked = True
    .Protect "abc"
  End With
End Sub
Note ThisWorkbook EVENT
- Open the VB Editor (press Alt + F11).​
- Over in the Project Explorer, double click on ThisWorkbook.​
- In the white panel that then appears, paste the above code.​



----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Last edited:
Upvote 0
Solution
Dante,

One more question... Is there a simple way to add in that if a value is entered into a cell in the "AI" column then it locks that entire row? I other words once the "AI" column is signed off on then that row cannot be altered?

Thank you in advance
 
Upvote 0
I other words once the "AI" column is signed off on then that row cannot be altered?
Your request is not very clear, I need to know:
How are your cells locked before you press the button?
What do you want to do when the button is pressed?
Do you want to lock the row after modifying a cell in the AI column?
When AI column editing is enabled, can you also modify other columns?
When you press save, what do you want to happen?

You must consider all scenarios and describe them.

I'm going to give it a try 🧙‍♂️
Try the following:


VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  With Sheets("Sheet1")
    .Unprotect "abc"
    .Cells.Locked = True
    .Protect "abc"
  End With
End Sub


VBA Code:
Private Sub CommandButton1_Click()
  Dim pwd As Variant
  
  pwd = InputBox("Please Enter password", "EDIT AI COLUMN")
  
  If StrPtr(pwd) = 0 Then
    MsgBox "Cancelled"
  ElseIf Len(pwd) = 0 Then
    MsgBox "Please Enter a valid value"
  Else
    If pwd = "xyz" Then
      With Sheets("Sheet1")
        .Unprotect "abc"
        .Range("AI" & .Range("AI" & Rows.Count).End(3).Row + 1, .Range("AI" & Rows.Count)).Locked = False
        .Protect "abc"
      End With
      MsgBox "Now you can edit the AI column"
    End If
  End If
End Sub

Note: you already know where to put each code. :cool:


If it is not what you need, you should create a new thread and explain in detail what you need, since it is a different topic than the one requested in the original post.

----- --
I hope to hear from you soon.
Respectfully
Dante Amor
----- --
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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