Toggle Hide/Unhide Macro with Button

ttbuson

Board Regular
Joined
Nov 18, 2011
Messages
80
I have the following macro for hiding/unhiding a group of rows based on whether or not they contain empty text strings. I was hoping to insert a button that would hide or unhide the rows each time you hit it. However, I haven't done much work with buttons and assigning controls. Any help would be greatly appreciated.

Thanks!

Code:
Sub HideRows()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
Dim cell As Range<o:p></o:p>
<o:p> </o:p>
For Each cell In ActiveSheet.Range("B114:B143 ")<o:p></o:p>
If cell.Value = "" Then<o:p></o:p>
cell.EntireRow.Hidden = True<o:p></o:p>
Else: cell.EntireRow.Hidden = False<o:p></o:p>
End If<o:p></o:p>
Next cell<o:p></o:p>
<o:p> </o:p>
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Figured it out. Assigning the following macro to the button works:

Code:
 [/COLOR]
[COLOR=#1f497d][COLOR=black]Sub HideRows()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/COLOR][/COLOR]
[COLOR=#1f497d]<o:p>[COLOR=black] [/COLOR]</o:p>[/COLOR]
[COLOR=#1f497d][COLOR=black]Dim cell As Range<o:p></o:p>[/COLOR][/COLOR]
[COLOR=#1f497d]<o:p>[COLOR=black] [/COLOR]</o:p>[/COLOR]
[COLOR=#1f497d][COLOR=black]For Each cell In ThisWorkbook.Sheets("GraphData").Range("B114:B143")<o:p></o:p>[/COLOR][/COLOR]
[COLOR=#1f497d][COLOR=black]    If cell.Value = "" Then<o:p></o:p>[/COLOR][/COLOR]
[COLOR=#1f497d][COLOR=black]        cell.EntireRow.Hidden = Not cell.EntireRow.Hidden<o:p></o:p>[/COLOR][/COLOR]
[COLOR=#1f497d][COLOR=black]        Else: cell.EntireRow.Hidden = False<o:p></o:p>[/COLOR][/COLOR]
[COLOR=#1f497d][COLOR=black]    End If<o:p></o:p>[/COLOR][/COLOR]
[COLOR=#1f497d][COLOR=black]Next cell<o:p></o:p>[/COLOR][/COLOR]
[COLOR=#1f497d]<o:p>[COLOR=black] [/COLOR]</o:p>[/COLOR]
[COLOR=#1f497d][COLOR=black]End Sub[/COLOR][/COLOR]
[COLOR=black]
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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