Trying to unlock all worksheets using a button form control

LabLady11

New Member
Joined
Oct 22, 2021
Messages
26
Office Version
  1. 2016
Platform
  1. Windows
Hi all!

I'm running the following code using a 'one-click button' and want to add the 'unprotect_all_sheets' code below to be the first code run when clicked. When I add it to the one-click button codes it will not work; if I use it independently the code works fine.

CODE THAT WORKS:
VBA Code:
Private Sub CommandButton1_Click()
End Sub

Sub Dosomething()
Dim xSh As Worksheet
    Application.ScreenUpdating = False
For Each xSh In Worksheets
    If xSh.Visible = xlSheetVisible Then Call RunCode(xSh)
Next
Application.ScreenUpdating = True
End Sub


Sub RunCode(sht As Worksheet)

Dim lr As Long, e As Range
With sht
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    On Error Resume Next
    For Each e In .Range("A1:A" & lr).SpecialCells(xlFormulas)
        If Len(e) = 0 Then .Rows(e.Row).Hidden = True
    Next
    On Error GoTo 0
End With
End Sub

TRYING TO ADD THIS TO THE ONE-CLICK CONTROL:
VBA Code:
Sub Unprotect_All_Sheets()
'declare a variable
Dim ws As Worksheet
'loop through each worksheet in this workbook and unprotect them using the same password that was used to protect them
For Each ws In ThisWorkbook.Worksheets
ws.Unprotect Password:="correct pw entered here"
Next ws

End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Why not just use
VBA Code:
Sub Dosomething()
Dim xSh As Worksheet
    Application.ScreenUpdating = False
For Each xSh In Worksheets
    xSh.Unprotect "correct pw entered here"
    If xSh.Visible = xlSheetVisible Then Call RunCode(xSh)
Next
Application.ScreenUpdating = True
End Sub
rather than having a separate sub.
 
Upvote 0
Solution
I tried that but the rows still won't hide if the sheet is locked. maybe i'm updating the click-control incorrectly.
 
Upvote 0
In future please do not put your reply inside quotes, as it looks as though you have simply quoted another member.

How are you running the DoSomething code?
 
Upvote 0
In future please do not put your reply inside quotes, as it looks as though you have simply quoted another member.

How are you running the DoSomething code?
apologies about that, I must have clicked on 'quote' accidentally.

The code is run as a worksheet action on a worksheet entitled "initial evaluation"
 
Upvote 0
What is the code that calls the DoSomething Sub?
 
Upvote 0

Forum statistics

Threads
1,215,700
Messages
6,126,302
Members
449,308
Latest member
VerifiedBleachersAttendee

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