Failed macro because sheet protected

willisbr

New Member
Joined
Mar 2, 2006
Messages
29
I have a protected sheet that I have a toggle button in. The toggle hides and unhides columns. The code failes and the toggle wont work if the sheet is protected. Is there a way to build the protect/unprotect within the toggle also? I've tried a few things but so far no good.
The only reason I have the sheet protected is to prevent a function from being overwritten or deleted...so if anything, I thought I can get the toggle to unprotect the sheet...do what it needs to do...and then reprotect again.

I'm using Excel 97

here is the current toggle code:

Private Sub CommandButton1_Click()

a = InputBox("Enter password:", "Security")
If a <> "password" Then
MsgBox "The password you entered is incorrect."
End
End If
With CommandButton1
If CommandButton1.Caption = "Hide Raw Data" Then

Range("C:D,F:G,I:J,M:P,R:S,U:V,X:Y,AA:AE,AG:AH").Select
Selection.EntireColumn.Hidden = True

CommandButton1.Caption = "Show Raw Data"
Else
Range("C:D,F:G,I:J,M:P,R:S,U:V,X:Y,AA:AE,AG:AH").Select
Selection.EntireColumn.Hidden = False
Range("C5").Select
CommandButton1.Caption = "Hide Raw Data"
End If
End With
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hello willisbr,
Certainly. You only need to add a couple of lines.
Code:
Private Sub CommandButton1_Click()

a = InputBox("Enter password:", "Security")
If a <> "password" Then
  MsgBox "The password you entered is incorrect."
  End
End If

ActiveSheet.Unprotect

With CommandButton1
  If CommandButton1.Caption = "Hide Raw Data" Then
    Range("C:D,F:G,I:J,M:P,R:S,U:V,X:Y,AA:AE,AG:AH").Select
    Selection.EntireColumn.Hidden = True
    CommandButton1.Caption = "Show Raw Data"
  Else
    Range("C:D,F:G,I:J,M:P,R:S,U:V,X:Y,AA:AE,AG:AH").Select
    Selection.EntireColumn.Hidden = False
    Range("C5").Select
    CommandButton1.Caption = "Hide Raw Data"
  End If
End With

ActiveSheet.Protect

End Sub
Note: If you're using a password on your sheet protection then you'll want to change
those two new lines to:
ActiveSheet.Unprotect "Your Password"
and
ActiveSheet.Protect "Your Password"

That's all you should need to do.
 
Upvote 0
I have a protected sheet that I have a toggle button in. The toggle hides and unhides columns. The code failes and the toggle wont work if the sheet is protected. Is there a way to build the protect/unprotect within the toggle also? I've tried a few things but so far no good.
The only reason I have the sheet protected is to prevent a function from being overwritten or deleted...so if anything, I thought I can get the toggle to unprotect the sheet...do what it needs to do...and then reprotect again.

I'm using Excel 97

here is the current toggle code:

I tend to avoid the use of vba to change a sheet's protection, especially without defining the workbook and sheet by name. The reason in my case is that I run many different workbooks, some at the same time and avoid having the code in one having the possibility of trying to operate on the wrong 'Active Sheet'. In any case have you tried just protecting the sheet and allowing 'Format'. I don't run 97 any more so not sure of the options for sheet protection.

In other versions when you protect a sheet you get a set of checkboxs for selecting what operations you allow or disallow on a sheet. I believe this is often a better solution then protecting or unprotecting a sheet., unless there are other reasons to do so.

Perry
 
Upvote 0
It protects it ok but when I get a run time error 1004 "unprotect method of worksheet class failed" when I click it again with it protected.

Any ideas? version issue?

Private Sub CommandButton1_Click()

a = InputBox("Enter password:", "Security")
If a <> "password" Then
MsgBox "The password you entered is incorrect."
End
End If

ActiveSheet.Unprotect

With CommandButton1
If CommandButton1.Caption = "Hide Raw Data" Then
Range("C:D,F:G,I:J,M:P,R:S,U:V,X:Y,AA:AE,AG:AH").Select
Selection.EntireColumn.Hidden = True
CommandButton1.Caption = "Show Raw Data"
Else
Range("C:D,F:G,I:J,M:P,R:S,U:V,X:Y,AA:AE,AG:AH").Select
Selection.EntireColumn.Hidden = False
Range("C5").Select
CommandButton1.Caption = "Hide Raw Data"
End If
End With

ActiveSheet.Protect

End Sub
 
Upvote 0
Well, it's been a while since I've used xl '97, I wrote that in xl '03, and am testing it here
with xl 2K. (and it's working fine.)
ActiveSheet.Unprotect (if memory serves) used to work fine in '97 as well so I wouldn't think
it's a version issue.

I assume when you get the error and hit Debug that it's the line ActiveSheet.Unprotect that's highlighted in yellow. (yes?)
I can't think of any reason off hand why you'd be getting that error. :confused:
 
Upvote 0
Well, it's been a while since I've used xl '97, I wrote that in xl '03, and am testing it here
with xl 2K. (and it's working fine.)
ActiveSheet.Unprotect (if memory serves) used to work fine in '97 as well so I wouldn't think
it's a version issue.

I assume when you get the error and hit Debug that it's the line ActiveSheet.Unprotect that's highlighted in yellow. (yes?)
I can't think of any reason off hand why you'd be getting that error. :confused:

I believe you would get this error in a macro if you try to change the worksheet format if the sheet is protected. This is why I mentioned checking the box marked 'Format', there is 'Format Rows', 'Format Columns', and I think 'Format Cells'. Not sure if this applies to 97 however.

Perry
 
Upvote 0
Unless I'm wrong, the error seems to be occurring while trying to unprotect the sheet as
opposed to trying to change any formatting while the sheet's protected, otherwise it
would likely be the "Unable to set the Hidden property of the range class" error.
 
Upvote 0
Unless I'm wrong, the error seems to be occurring while trying to unprotect the sheet as
opposed to trying to change any formatting while the sheet's protected, otherwise it
would likely be the "Unable to set the Hidden property of the range class" error.

I was paying attention to 1004, so I think you are probably correct. One thing to try with 97 is to first select the sheet to make it truely active within the macro and then run the unprotect routine. I am not sure of this in 97 but if run from a command button the button may be taking the focus away from the sheet leaving no active sheet. Without having 97 can the control be set to not take focus on click?

Perry
 
Upvote 0
Perfect thats what it was!! Allright :)

Lastly, can the password type with a security feature? IE: ****** instead of the actual text showing as I type it in?
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,247
Members
448,879
Latest member
oksanana

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