VBA Help! How to enable toggle button in a protected sheet

xlingg

New Member
Joined
Jul 21, 2020
Messages
6
Platform
  1. Windows
Hi there, I am a really new to VBA and I'm trying to insert a toggle button to hide/unhide columns but it has to be protected.

The code that I found protect/unprotect upon clicking the button to hide/unhide which is not what I want.

I need the toggle button to just hide/unhide in a protected sheet which cannot be unprotected by other users using the file.

Below is the full code:

VBA Code:
'Name macro
Sub HideColumn()

ActiveSheet.Unprotect Password:="123"

'Check if column F is hidden
If Range("A:CC").EntireColumn.Hidden = True Then

 
    'Show column F
    Range("A:D").EntireColumn.Hidden = False
    Range("F:G").EntireColumn.Hidden = False
    Range("J:K").EntireColumn.Hidden = False
    Range("M:q").EntireColumn.Hidden = False
    Range("s:u").EntireColumn.Hidden = False
    Range("w:y").EntireColumn.Hidden = False
    Range("AA:AD").EntireColumn.Hidden = False
    Range("Ag:Ai").EntireColumn.Hidden = False
    Range("Ak:Ak").EntireColumn.Hidden = False
    Range("Am:Aq").EntireColumn.Hidden = False
    Range("Ax:bi").EntireColumn.Hidden = False
    Range("bk:bl").EntireColumn.Hidden = False
    Range("bn:bo").EntireColumn.Hidden = False
    Range("bq:br").EntireColumn.Hidden = False
    
 
'Continue here if column F is visible
Else
 
    'Hide column F
    Range("A:D").EntireColumn.Hidden = True
    Range("F:G").EntireColumn.Hidden = True
    Range("J:K").EntireColumn.Hidden = True
    Range("M:q").EntireColumn.Hidden = True
    Range("s:u").EntireColumn.Hidden = True
    Range("w:y").EntireColumn.Hidden = True
    Range("AA:AD").EntireColumn.Hidden = True
    Range("Ag:Ai").EntireColumn.Hidden = True
    Range("Ak:Ak").EntireColumn.Hidden = True
    Range("Am:Aq").EntireColumn.Hidden = True
    Range("Ax:bi").EntireColumn.Hidden = True
    Range("bk:bl").EntireColumn.Hidden = True
    Range("bn:bo").EntireColumn.Hidden = True
    Range("bq:br").EntireColumn.Hidden = True
    
ActiveSheet.Protect Password:="123"

End If

End Sub

Appreciate any help please! Thank you! :)
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You can also enable filter option at the time of protecting Excel Sheet by selecting "Use Autofilter" options.
 
Upvote 0
@xlingg
Hi & welcome to MrExcel.
How about
VBA Code:
Sub xlingg()
   ActiveSheet.Unprotect Password:="123"
   With Range("A:D,F:G,J:K,M:Q,S:U,W:Y,AA:AD,AG:AI,AK:AK,AM:AQ,AX:BI,BK:BL,BN:BO,BQ:BR")
      .EntireColumn.Hidden = Not .EntireColumn.Hidden
   End With
   ActiveSheet.Protect Password:="123"
End Sub
 
Upvote 0
@xlingg
Hi & welcome to MrExcel.
How about
VBA Code:
Sub xlingg()
   ActiveSheet.Unprotect Password:="123"
   With Range("A:D,F:G,J:K,M:Q,S:U,W:Y,AA:AD,AG:AI,AK:AK,AM:AQ,AX:BI,BK:BL,BN:BO,BQ:BR")
      .EntireColumn.Hidden = Not .EntireColumn.Hidden
   End With
   ActiveSheet.Protect Password:="123"
End Sub

OMG!! Thank you so much, Fluff!! It worked!! I have been working on this for a week and your reply just solved my problem! I really appreciate your help! It made my day! :love:
 
Upvote 0
Hi!

Following @Fluff help, I am wondering what code can be added to allow filter in the protected worksheet? Thanks again!
 
Upvote 0
I would also need to allow users to fill in the worksheet even though it is protected :geek:
 
Upvote 0
To allow filtering add an autofilter before protecting the sheet, then record a macro of you protecting the sheet & check the box marked "use autofilter" & you will have the code.
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,144
Members
448,552
Latest member
WORKINGWITHNOLEADER

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