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

Alfredathavan

New Member
Joined
Jun 19, 2021
Messages
1
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
  2. Web
Hi 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.

Below is the full code:



Private Sub Choose_Click()

End Sub

Private Sub ToggleButton1_Click()
Dim xAddress As String
xAddress = "L:M"
If ToggleButton1.Value Then
Application.ActiveSheet.Columns(xAddress).Hidden = True
Else
Application.ActiveSheet.Columns(xAddress).Hidden = False
End If
End Sub

Private Sub ToggleButton2_Click()
Dim xAddress As String
xAddress = "O:U"
If ToggleButton2.Value Then
Application.ActiveSheet.Columns(xAddress).Hidden = True
Else
Application.ActiveSheet.Columns(xAddress).Hidden = False
End If
End Sub


Private Sub ToggleButton3_Click()
Dim xAddress As String
xAddress = "S"
If ToggleButton3.Value Then
Application.ActiveSheet.Columns(xAddress).Hidden = True
Else
Application.ActiveSheet.Columns(xAddress).Hidden = False
End If
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

EFANYoutube

Active Member
Joined
May 19, 2017
Messages
278
This is how you can protect the sheet from being changed by users but VBA code can still edit
VBA Code:
Sheet1.Protect userinterfaceonly:=True, Password:="MyPassword"
 

Forum statistics

Threads
1,140,930
Messages
5,703,228
Members
421,285
Latest member
Bebek

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