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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,226,735
Messages
6,192,733
Members
453,752
Latest member
Austin2222

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