VBA Code to Protect/Un-Protect Sheets asking for a password when there isn't one

bakom002

New Member
Joined
Aug 19, 2019
Messages
11
Hello Everyone,

I wrote some code to protect/unprotect all of the worksheets in my workbook, and when I run the code in the vba editor it works perfectly fine.

However, I added some shapes to my excel file and then assign the macros to those shapes, so the macro could be run at the click of a button, and now its asking me for a password when I run the macro from the button.

Also, entering the password used to lock the cells doesnt work. Here is the code:

Code:
Sub UnProtect()
'Dim ps As String


Application.ScreenUpdating = False
Application.Calculation = xlManual


'ps = "modeldevrocks"
For i = 1 To Sheets.count
    Sheets(i).UnProtect Password:="modeldevrocks"
Next i


Application.ScreenUpdating = True
Application.Calculation = xlAutomatic


End Sub

////////////////////////////////////////////////

Sub Protect()
'Dim ps As String


Application.ScreenUpdating = False
Application.Calculation = xlManual


'ps = "modeldevrocks"
For i = 1 To Sheets.count
    Sheets(i).Protect Password:="modeldevrocks"
Next i


Application.ScreenUpdating = True
Application.Calculation = xlAutomatic


End Sub

Thanks in advanced gals and guys!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You must use the method UserInterfaceOnly:=True
with can run The macro in protected sheet
like this example
Code:
Option Explicit


Sub Protect_sh()
Dim i
For i = 1 To Sheets.Count
    Sheets(i).Protect Password:="Pass", UserInterfaceOnly:=True
Next i
End Sub
'=====================
Sub test()
'test the Macro in protected sheet
Range("f4") = ""
Range("A1:A5") = "I'm Runing in protected sheet"
Columns(1).AutoFit
End Sub
 
Upvote 0
fgCRzTi
Hey thanks for the response, so I added the code, and it still works when I run the code form the editor, but I still get the "Password Required" pop-up box when I try to run it from the excel button, and the password used to lock/unlock the sheets won't work.
fgCRzTi


Here is the code now:
Code:
Option Explicit
Sub UnProtect()
Dim i


Application.ScreenUpdating = False
Application.Calculation = xlManual


For i = 1 To Sheets.count
    Sheets(i).UnProtect Password:="modeldevrocks"
Next i


Application.ScreenUpdating = False
Application.Calculation = xlManual


End Sub

//////////////////////////////////////////////////////////

Sub Protect()
Dim i


Application.ScreenUpdating = False
Application.Calculation = xlManual


For i = 1 To Sheets.count
    Sheets(i).Protect Password:="modeldevrocks", UserInterfaceOnly:=True
Next i


Application.ScreenUpdating = False
Application.Calculation = xlManual


End Sub
 
Upvote 0
That looks like it's an inputbox that is appearing, not the normal dialogue box.
Are you sure the code you've posted, is the code being run by the button?
 
Upvote 0
Yes I am fairly sure, I right clicked on the the button in excel and assigned the macro that I want to run.
 
Upvote 0
If you add the part in red as shown & the click the button, does the code window pop-up with the line highlighted in yellow?
Code:
Sub UnProtect()
Dim i
[COLOR=#ff0000]Stop[/COLOR]

Application.ScreenUpdating = False
Application.Calculation = xlManual


For i = 1 To Sheets.count
    Sheets(i).UnProtect Password:="modeldevrocks"
Next i


Application.ScreenUpdating = False
Application.Calculation = xlManual


End Sub
 
Upvote 0
Oh my goodness, you were right.... I didn't realize I had similarly named code in there, thank you so much lol
 
Upvote 0
try Do this
1- insert a CommandButton with name My_Button for example (ActiveX controls)
2 -try to run this macro by pressing the button
Code:
Sub test()
Sheets("sheet1").Protect Password:="pass", UserInterfaceOnly:=True
Range("A1:A5") = "Hallo"


End Sub
'============================


Private Sub My_Button_Click()
test
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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