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
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try this:
VBA Code:
Private Sub CommandButton1_Click()
'Modified  5/3/2022  11:51:26 PM  EDT
Range("Alpha").EntireRow.Hidden = Not Range("Alpha").EntireRow.Hidden
End Sub
 
Upvote 0
To simplly toggle the selected rows:
VBA Code:
Sub hiderows()
    Selection.EntireRow.Hidden = Not (Selection.EntireRow.Hidden)
End Sub

Selection is whatever cells already selected. ".EntireRow" then takes the whole of thoise rows (ratehr than selective columns that might have been selected within the rows). We can read the .Hidden attribute, use NOT to invert that and then set as the current hidden status

To state a range of lets say rows 5 to 7, rather than what has already been selected:
VBA Code:
Sub hiderows()
    Rows("5:7").Hidden = Not (Rows("5:7").Hidden)
End Sub

This is awkward coding, especially as defining "Rows("5:7") twice. Using "With ... End With" we can specify the object of interest:
VBA Code:
Sub hiderows()
    With Rows("5:7")
        .Hidden = Not (.Hidden)
    End With
End Sub

To define the required rows with variables:
VBA Code:
Sub hiderows()
Dim RowStart As Integer, RowEnd As Integer
    RowStart = 5
    RowEnd = 7
    With Rows(RowStart & ":" & RowEnd)
        .Hidden = Not (.Hidden)
    End With
End Sub

Lastly if already have a named area, say "NamedArea", then:
VBA Code:
Sub hiderows()
    With Range("NamedArea").EntireRow
            .Hidden = Not (.Hidden)
    End With
End Sub
 
Upvote 0
You said

Lastly if already have a named area, say "NamedArea", then:
VBA Code:
Sub hiderows()
With Range("NamedArea").EntireRow
.Hidden = Not (.Hidden)
End With
End Sub

Can we make it toggle for a named area? (Maybe I'm missing something above.)

Otherwise, to use the above as a separate macro for unhiding the rows, would I replace line 3 with

.Hidden = (.Hidden)
or
.Hidden = Yes (.Hidden)

Thanks!
 
Upvote 0
Above coding toggles ("Nots" the existing value), so just re-run teh code to revert back.
The .Hidden parameter takes values of "True" or "False" (non-zero number or zero), so as separate specific routines, rather than a toggle:

VBA Code:
Sub hider()
    Range("NamedArea").EntireRow.Hidden = True
End Sub

Sub unhider()
    Range("NamedArea").EntireRow.Hidden = False
End Sub
 
Upvote 0
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
 
Upvote 0
Above coding toggles ("Nots" the existing value), so just re-run teh code to revert back.
The .Hidden parameter takes values of "True" or "False" (non-zero number or zero), so as separate specific routines, rather than a toggle:

VBA Code:
Sub hider()
    Range("NamedArea").EntireRow.Hidden = True
End Sub

Sub unhider()
    Range("NamedArea").EntireRow.Hidden = False
End Su
[QUOTE="davidruben, post: 5878463, member: 60337"]
Above coding toggles ("Nots" the existing value), so just re-run teh code to revert back.
The .Hidden parameter takes values of "True" or "False" (non-zero number or zero), so as separate specific routines, rather than a toggle: 
 
[CODE=vba]Sub hider()
    Range("NamedArea").EntireRow.Hidden = True
End Sub

Sub unhider()
    Range("NamedArea").EntireRow.Hidden = False
End Sub
[/QUOTE]

I was surprised that the "above code" that I had originally complained about did everything I wanted, acting just like a toggle. However, I'm going to play with, and learn from, everyone's comments over the next few days, and then I'll offer my two cents.

What I DON'T know about Excel VBA, and even formulas, is so much more than I DO know. It's also SO LITTLE when compared to some of the mavens in this discussion group. ;) ;) ;)

Thanks,
Dave
 
Upvote 0

I was surprised that the "above code" that I had originally complained about did everything I wanted, acting just like a toggle. However, I'm going to play with, and learn from, everyone's comments over the next few days, and then I'll offer my two cents.

What I DON'T know about Excel VBA, and even formulas, is so much more than I DO know. It's also SO LITTLE when compared to some of the mavens in this discussion group. ;) ;) ;)

Thanks,
Dave
[/QUOTE]
Not sure what your saying here. My script Toggles a named range like I thought you asked for. But it's true with Excel there are 20 ways to do everything. So I gave you what i use.
 
Upvote 0

Forum statistics

Threads
1,215,558
Messages
6,125,507
Members
449,236
Latest member
Afua

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