Hide and Unhide Rows Macro

ExcelAverage

New Member
Joined
Dec 10, 2012
Messages
32
May I post this one query I am having regarding hiding and unhiding rows.
I have the following code to hide and unhide row 5-34. I attached the macro to one button. My problem is I sometimes needed to insert row/s between 5-34. How do I make my code flexible that it will adjust the number of rows if I inserted new ones (varying number of inserted row). Say 5-34 should be 5-39 if I added another 5 rows. Below is the code I found online, too. I am looking for that capability same as dynamic range.
Code:
Sub Hide_Unhide()    
        With Rows("5:34").Select
       .EntireRow.Hidden = Not .EntireRow.Hidden
        End With
End Sub

Thanks for the help
-JM
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
One way would be to use "named ranges". The following sample assumes that row 5 has been named "Start" and row 34 has been named "Finish". The name(s) will move up/down as rows are deleted/inserted.

Hope it helps.

Gary

Code:
Sub Hide_Unhide()

Dim oTarget As Range

Set oTarget = Range("Start:Finish")
oTarget.EntireRow.Hidden = Not oTarget.EntireRow.Hidden
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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