Macro to hide non-contiguous rows in excel

kwoifo

New Member
Joined
Apr 1, 2009
Messages
47
Hi,

I have a macro in a sheet that allows a user to hide some specific non-contiguous rows. It works, but is quite slow unfortunately. I assume there is a better way I can write the code to improve the speed. I am currently using this:

Sub Hide()
ActiveSheet.rows("16:17").Hidden = True
ActiveSheet.rows("19:20").Hidden = True
ActiveSheet.rows("22:23").Hidden = True
ActiveSheet.rows("27:28").Hidden = True
ActiveSheet.rows("30:31").Hidden = True
ActiveSheet.rows("33:33").Hidden = True
ActiveSheet.rows("35:35").Hidden = True
ActiveSheet.rows("38:39").Hidden = True
ActiveSheet.rows("41:41").Hidden = True
End Sub

Any suggestions on how to improve it? Currently takes about 5secs for all the rows to be hidden :(

Many thanks!!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi,

I have a macro in a sheet that allows a user to hide some specific non-contiguous rows. It works, but is quite slow unfortunately. I assume there is a better way I can write the code to improve the speed. I am currently using this:

Sub Hide()
ActiveSheet.rows("16:17").Hidden = True
ActiveSheet.rows("19:20").Hidden = True
ActiveSheet.rows("22:23").Hidden = True
ActiveSheet.rows("27:28").Hidden = True
ActiveSheet.rows("30:31").Hidden = True
ActiveSheet.rows("33:33").Hidden = True
ActiveSheet.rows("35:35").Hidden = True
ActiveSheet.rows("38:39").Hidden = True
ActiveSheet.rows("41:41").Hidden = True
End Sub

Any suggestions on how to improve it? Currently takes about 5secs for all the rows to be hidden :(

Many thanks!!
Use the Application.Union method to form a non-contiguous range. Then hide that range.
Code:
Dim R as range
Set R = Union(ActiveSheet.rows("16:17"),ActiveSheet.rows("19:20"), ...)
R.hidden = True
 
Upvote 0
Joe -- thanks a lot for the quick reply!!

I have tried your solution but get runtime error 1004 (application-defined or object-defined error) ? Am I doing something wrong? Many thanks
 
Upvote 0
Try it like this...

Code:
Rows("16:39").Hidden = True
Range("18:18,21:21,24:26,29:29,32:32,34:34,36:37").EntireRow.Hidden = False
 
Upvote 0
Joe -- thanks a lot for the quick reply!!

I have tried your solution but get runtime error 1004 (application-defined or object-defined error) ? Am I doing something wrong? Many thanks
Sorry, that should be R.EntireRow.Hidden = True
 
Upvote 0
Rick that does the trick! Absolutely fantastic! Many thanks!!!

You are welcome; however, I would rather you do it this way instead. Originally, I thought the two-step method would be more compact, but doing the hiding in one step directly is not all that much longer. So I would do it with this single line of code instead of the two lines of code I gave you earlier...

Code:
Range("16:17,19:20,22:23,27:28,30:31,33:33,35:35,38:39,41:41").EntireRow.Hidden = True
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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