Shorter code to hide/unhide rows with a VBA button

aylafan

New Member
Joined
Apr 13, 2011
Messages
20
Is there a shorter code I can use to hide/unhide rows with a VBA button?

There are no "fixed" range number of rows between Header to Header; they are random. Example: There are less rows between Header 1 to Header 2 then there are rows between Header 2 to Header 3, etc.

Header 1 at (A2:K2)

range: (A4:K190)

***hide every 6 rows*** (A4:K9), (A11:K16),(A18:K23), etc.

***then hide 5 rows range*** (A186:K190)

Header 2 at (A191:K191)

range: (A193:K386)

***hide every 6 rows, range:*** (A193:K198), (A200:K205),(A207:K212), etc.

***then hide 5 rows range*** (A382:K386)

Header 3 at (A387:K387)

range: (A389:K582)

***hide every 6 rows*** (A389:K394), (A396:K401), (A403:K408), etc.

***then hide 5 rows range*** (A578:K582)

Header 4 at (A583:K583)

range: (A585:K701)

***hide every 6 rows*** (A585:K589), (A592:K597), (A599:K604), etc.

***then hide 5 rows range*** (A697:K701)

Here is the code I used, but I had to type in every range for it to work correctly.

Code:
Sub Hidden()
Application.ScreenUpdating = False
Rows("4:9").Hidden = Not Rows("4:9").Hidden
Rows("11:16").Hidden = Not Rows("11:16").Hidden
Rows("18:23").Hidden = Not Rows("18:23").Hidden
Rows("25:30").Hidden = Not Rows("25:30").Hidden
Rows("32:37").Hidden = Not Rows("32:37").Hidden
Rows("39:44").Hidden = Not Rows("39:44").Hidden
Rows("46:51").Hidden = Not Rows("46:51").Hidden
Rows("53:58").Hidden = Not Rows("53:58").Hidden
Rows("60:65").Hidden = Not Rows("60:65").Hidden
Rows("67:72").Hidden = Not Rows("67:72").Hidden
Rows("74:79").Hidden = Not Rows("74:79").Hidden
Rows("81:86").Hidden = Not Rows("81:86").Hidden
Rows("88:93").Hidden = Not Rows("88:93").Hidden
Rows("95:100").Hidden = Not Rows("95:100").Hidden
Rows("102:107").Hidden = Not Rows("102:107").Hidden
Rows("109:114").Hidden = Not Rows("109:114").Hidden
Rows("116:121").Hidden = Not Rows("116:121").Hidden
Rows("123:128").Hidden = Not Rows("123:128").Hidden
Rows("130:135").Hidden = Not Rows("130:135").Hidden
Rows("137:142").Hidden = Not Rows("137:142").Hidden
Rows("144:149").Hidden = Not Rows("144:149").Hidden
Rows("151:156").Hidden = Not Rows("151:156").Hidden
Rows("158:163").Hidden = Not Rows("158:163").Hidden
Rows("165:170").Hidden = Not Rows("165:170").Hidden
Rows("172:177").Hidden = Not Rows("172:177").Hidden
Rows("179:184").Hidden = Not Rows("179:184").Hidden
Rows("186:190").Hidden = Not Rows("186:190").Hidden
Rows("193:198").Hidden = Not Rows("193:198").Hidden
Rows("200:205").Hidden = Not Rows("200:205").Hidden
Rows("207:212").Hidden = Not Rows("207:212").Hidden
Rows("214:219").Hidden = Not Rows("214:219").Hidden
Rows("221:226").Hidden = Not Rows("221:226").Hidden
Rows("228:233").Hidden = Not Rows("228:233").Hidden
Rows("235:240").Hidden = Not Rows("235:240").Hidden
Rows("242:247").Hidden = Not Rows("242:247").Hidden
Rows("249:254").Hidden = Not Rows("249:254").Hidden
Rows("256:261").Hidden = Not Rows("256:261").Hidden
Rows("263:268").Hidden = Not Rows("263:268").Hidden
Rows("270:275").Hidden = Not Rows("270:275").Hidden
Rows("277:282").Hidden = Not Rows("277:282").Hidden
Rows("284:289").Hidden = Not Rows("284:289").Hidden
Rows("291:296").Hidden = Not Rows("291:296").Hidden
Rows("298:303").Hidden = Not Rows("298:303").Hidden
Rows("305:310").Hidden = Not Rows("305:310").Hidden
Rows("312:317").Hidden = Not Rows("312:317").Hidden
Rows("319:324").Hidden = Not Rows("319:324").Hidden
Rows("326:331").Hidden = Not Rows("326:331").Hidden
Rows("333:338").Hidden = Not Rows("333:338").Hidden
Rows("340:345").Hidden = Not Rows("340:345").Hidden
Rows("347:352").Hidden = Not Rows("347:352").Hidden
Rows("354:359").Hidden = Not Rows("354:359").Hidden
Rows("361:366").Hidden = Not Rows("361:366").Hidden
Rows("368:373").Hidden = Not Rows("368:373").Hidden
Rows("375:380").Hidden = Not Rows("375:380").Hidden
Rows("382:386").Hidden = Not Rows("382:386").Hidden
Rows("389:394").Hidden = Not Rows("389:394").Hidden
Rows("396:401").Hidden = Not Rows("396:401").Hidden
Rows("403:408").Hidden = Not Rows("403:408").Hidden
Rows("410:415").Hidden = Not Rows("410:415").Hidden
Rows("417:422").Hidden = Not Rows("417:422").Hidden
Rows("424:429").Hidden = Not Rows("424:429").Hidden
Rows("431:436").Hidden = Not Rows("431:436").Hidden
Rows("438:443").Hidden = Not Rows("438:443").Hidden
Rows("445:450").Hidden = Not Rows("445:450").Hidden
Rows("452:457").Hidden = Not Rows("452:457").Hidden
Rows("459:464").Hidden = Not Rows("459:464").Hidden
Rows("466:471").Hidden = Not Rows("466:471").Hidden
Rows("473:478").Hidden = Not Rows("473:478").Hidden
Rows("480:485").Hidden = Not Rows("480:485").Hidden
Rows("487:492").Hidden = Not Rows("487:492").Hidden
Rows("494:499").Hidden = Not Rows("494:499").Hidden
Rows("501:506").Hidden = Not Rows("501:506").Hidden
Rows("508:513").Hidden = Not Rows("508:513").Hidden
Rows("515:520").Hidden = Not Rows("515:520").Hidden
Rows("522:527").Hidden = Not Rows("522:527").Hidden
Rows("529:534").Hidden = Not Rows("529:534").Hidden
Rows("536:541").Hidden = Not Rows("536:541").Hidden
Rows("543:548").Hidden = Not Rows("543:548").Hidden
Rows("550:555").Hidden = Not Rows("550:555").Hidden
Rows("557:562").Hidden = Not Rows("557:562").Hidden
Rows("564:569").Hidden = Not Rows("564:569").Hidden
Rows("571:576").Hidden = Not Rows("571:576").Hidden
Rows("578:582").Hidden = Not Rows("578:582").Hidden
Rows("585:590").Hidden = Not Rows("585:590").Hidden
Rows("592:597").Hidden = Not Rows("592:597").Hidden
Rows("599:604").Hidden = Not Rows("599:604").Hidden
Rows("606:611").Hidden = Not Rows("606:611").Hidden
Rows("613:618").Hidden = Not Rows("613:618").Hidden
Rows("620:625").Hidden = Not Rows("620:625").Hidden
Rows("627:632").Hidden = Not Rows("627:632").Hidden
Rows("634:639").Hidden = Not Rows("634:639").Hidden
Rows("641:646").Hidden = Not Rows("641:646").Hidden
Rows("648:653").Hidden = Not Rows("648:653").Hidden
Rows("655:660").Hidden = Not Rows("655:660").Hidden
Rows("662:667").Hidden = Not Rows("662:667").Hidden
Rows("669:674").Hidden = Not Rows("669:674").Hidden
Rows("676:681").Hidden = Not Rows("676:681").Hidden
Rows("683:688").Hidden = Not Rows("683:688").Hidden
Rows("690:695").Hidden = Not Rows("690:695").Hidden
Rows("697:701").Hidden = Not Rows("697:701").Hidden
Application.ScreenUpdating = True
End Sub

--------------------------------------------------
Excel 2007 and Windows XP Professional
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I did not test this macro, but I believe I did all the math correctly (if not, let me know where and how it fails)...
Code:
Sub Hidden()
  Dim X As Long
  Application.ScreenUpdating = False
  For X = 4 To 186 Step 7
    Rows(X & ":" & (X + 5 + (X = 186))).Hidden = Not Rows(X & ":" & (X + 5 + (X = 186))).Hidden
    Rows((X + 189) & ":" & (X + 194 + (X = 186))).Hidden = Not Rows((X + 189) & ":" & (X + 194 + (X = 186))).Hidden
    Rows((X + 385) & ":" & (X + 398 + (X = 186))).Hidden = Not Rows((X + 393) & ":" & (X + 398 + (X = 186))).Hidden
    Rows((X + 581) & ":" & (X + 585 + (X = 186))).Hidden = Not Rows((X + 581) & ":" & (X + 585 + (X = 186))).Hidden
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
The code works great until get it gets to row 380 and shows rows 380-388.

Then hides rows 389-583.

Then it shows row 584 then hides rows 585-589 and shows rows 590-591. It repeats this pattern throughout.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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