Speed up VBA Code to Hide Rows

tlc53

Active Member
Joined
Jul 26, 2018
Messages
399
Hi there,

This VBA code has been super useful in many spreadsheets but on this occasion, it is running very slow. It takes 10 minutes to complete!
The reason is that my range is usually only about 15 cells but now it needs to check 100 cells.

Is there any way to improve/speed up this code?

Thank you!

VBA Code:
Sub HideRows()
   Dim Cl As Range
   For Each Cl In Range("D21:D120")
     Cl.EntireRow.Hidden = IIf(Cl = 0, True, False)
   Next Cl
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
That code should take well under a second, even if hiding all the rows.
However you can get rid of the IIF
VBA Code:
Sub HideRows()
   Dim Cl As Range
   For Each Cl In Range("D21:D120")
     Cl.EntireRow.Hidden = Cl = 0
   Next Cl
End Sub
Do you have lots of formulae on that sheet?
 
Upvote 0
That code should take well under a second, even if hiding all the rows.
However you can get rid of the IIF
VBA Code:
Sub HideRows()
   Dim Cl As Range
   For Each Cl In Range("D21:D120")
     Cl.EntireRow.Hidden = Cl = 0
   Next Cl
End Sub
Do you have lots of formulae on that sheet?
Yes, there are lots of formulas but I amended the code as above and it ran super fast. About 20 seconds!!
Thank you! :)
 
Upvote 0
If you turn calculation, it might speed it up a bit more.
VBA Code:
Sub HideRows()
   Dim Cl As Range, t
   Application.Calculation = xlCalculationManual
   For Each Cl In Range("D21:D120")
     Cl.EntireRow.Hidden = Cl = 0
   Next Cl
   Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
Solution
If you turn calculation, it might speed it up a bit more.
VBA Code:
Sub HideRows()
   Dim Cl As Range, t
   Application.Calculation = xlCalculationManual
   For Each Cl In Range("D21:D120")
     Cl.EntireRow.Hidden = Cl = 0
   Next Cl
   Application.Calculation = xlCalculationAutomatic
End Sub
Holy heck! This code blew the other code out the window ? Thank you!
 
Upvote 0
You could also turn off screenupdating
VBA Code:
Sub HideRows()
   Dim Cl As Range, t
with Application
     .Calculation = xlCalculationManual
     .screenupdating=false
end with
   For Each Cl In Range("D21:D120")
     Cl.EntireRow.Hidden = Cl = 0
   Next Cl
with Application
     .Calculation = xlCalculationautomatic
     .screenupdating=true
end with
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,713
Messages
6,174,039
Members
452,542
Latest member
Bricklin

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