Toggle for Hiding or Unhiding a Few Specific or Named Rows

daveG777

New Member
Joined
May 2, 2019
Messages
30
Office Version
  1. 365
Platform
  1. Windows
I have very simple VBA that hides or unhides rows that are already highlighted.

However, I need a toggle, so that the same code will hide or unhide specific rows. Also, I'd prefer to hide or unhide a named range of rows, if that's possible, rather than just give the usual column letters and row numbers.

Here's as far as I got with using simple memorized keystrokes:
_____________________________________________

Sub rowsHide()​
'Unprotect sheet​
ActiveSheet.Unprotect​
'Hide rows that are highlighted​
Selection.EntireRow.Hidden = True​
End Sub
_____________________________________________​

Sub rowsUnhide()​
'Unprotect sheet​
ActiveSheet.Unprotect​
'Unhide rows that are highlighted​
Selection.EntireRow.Hidden = False​
End Sub​
_____________________________________________

Here's what someone else did, but I don't understand or need the complications with the button, or about the stuff on a different sheet.

By onthegreen03​

Hi - I set up a toggle button (ActiveX Controls) that hides/unhides columns, code is attached below. How would I change the code if I wanted to hide/unhide the same columns but on a different sheet? I want to keep the toggle button on the active sheet (let's call that "Sheet 1") and when pressed will hide/unhide columns on "Sheet 2". Thanks for your help!​
Private Sub ToggleButton1_Click()​
Dim xAddress As String
xAddress = "C:N"
If ToggleButton1.Value Then
Application.ActiveSheet.Columns(xAddress).Hidden = True
ToggleButton1.Caption = "Show Months"
Else
Application.ActiveSheet.Columns(xAddress).Hidden = False
ToggleButton1.Caption = "Hide Months"
End If
End Sub​
_____________________________________________
By the way, I'm really grateful when people write comments to explain their syntax or logic.

Thanks,
Dave
 
If the range is on another sheet use this:
VBA Code:
Private Sub CommandButton1_Click()
'Modified  5/4/2022  12:09:53 AM  EDT
Sheets("Alpha").Range("Cake").EntireColumn.Hidden = Not Sheets("Alpha").Range("Cake").EntireColumn.Hidden
End Sub
Here is my code to Toggle a range on a specific sheet.
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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