Macro to protect/unprotect sheet and book

mrMadCat

New Member
Joined
Jun 8, 2016
Messages
39
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hello, I'm trying to run quite a simple macro but it doesn't work and says "object not defined".

The macro is run when the ToggleButton is pressed. So:
Sheet1:
Code:
Private Sub ToggleButton1_Click()    Protect_and_Structure
End Sub

Module1
Code:
Sub Protect_and_Structure(wsSh As Worksheet)    Dim wsSh As Object
    For Each wsSh In Me.Worksheets
        If Me.ToggleButton1.Value = True Then
            wsSh.Unprotect "PASS"
            wsSh.EnableOutlining = True
            wsSh.Protect Password:="PASS", Contents:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True, UserinterfaceOnly:=True
        Else
            wsSh.Unprotect
    Next wsSh
'    ActiveWorkbook.Unprotect
    End If
End Sub

What I do wrong?

I also have a problem with "ActiveWorkbook.Unprotect" not asking for the password the way wsSh.Unprotect does. The only way is to manually create input box and all the error checks?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,423
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
The declaration line should read:

Code:
Sub Protect_and_Structure()
and not:
Code:
Sub Protect_and_Structure(wsSh As Worksheet)
 
Upvote 0

mrMadCat

New Member
Joined
Jun 8, 2016
Messages
39
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Now I have another error: Invalid use of Me statement.

I've changed module1 to following but it still doesn't work saying "Next without For" error:
Code:
Sub Protect_and_Structure()
     Dim wsSh As Object
    For Each wsSh In ActiveWorkbook.Worksheets
        If ToggleButton1.Value = True Then
            wsSh.Unprotect "PASS"
            wsSh.EnableOutlining = True
            wsSh.Protect Password:="PASS", Contents:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True, UserinterfaceOnly:=True
        Else
            wsSh.Unprotect
    Next wsSh
'    ActiveWorkbook.Unprotect
    End If
End Sub
 
Last edited:
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,423
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Your End If needs to come before the Next wsSh line, and you should specify where ToggleButton1 is:

Code:
Sub Protect_and_Structure()
     Dim wsSh As Object
    For Each wsSh In ActiveWorkbook.Worksheets
        If ActiveSheet.ToggleButton1.Value = True Then
            wsSh.Unprotect "PASS"
            wsSh.EnableOutlining = True
            wsSh.Protect Password:="PASS", Contents:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True, UserinterfaceOnly:=True
        Else
            wsSh.Unprotect
        End If
    Next wsSh
'    ActiveWorkbook.Unprotect

End Sub
 
Upvote 0

Forum statistics

Threads
1,191,517
Messages
5,987,057
Members
440,074
Latest member
Emmanuelian

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
Top