Hide Columns Macro Executes Slowly

ringmaker

Board Regular
Joined
Oct 26, 2003
Messages
75
Hello - I have a simple macro that looks at a named range which is one row of the spreadsheet from columns 1 to 100. If an "x" is found, that column should be hidden. This code executes extremely slowly. I am evaluating a range with 100 columns but I could do this faster manually. Is there a way to speed this up? Thanks!!!

'Hide Columns with an X
For Each HideRange In Me.Range("HideRange")
If HideRange = "x" Then
HideRange.Columns.Hidden = True
End If
Next

I also tried this another way with the same speed issue....

For Each HideRange In Me.Range("HideRange")
If HideRange = "x" Then
hc = HideRange.Column
Columns(hc).Hidden = True
End If
Next
 

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.
the first thing you could do is

"Application.ScreenUpdating = False" at the start of the macro

"Application.ScreenUpdating = True" at the end at the end of the macro
 
Upvote 0
Same recommendation as above.

But your code shouldn't take very long to run with or without screen updating turned on.

You don't happen to have subtotal or some other volatile function across all those columns?

Maybe add:
Application.Calculation = xlCalculationManual
' code
Application.Calculation = xlCalculationAutomatic
 
Upvote 0
Thanks to both of you. I actually do have a line of code to set ScreenUpdating to False and calculation is set to manual (i just gave you the section of code that was slow). Any other thoughts? Thanks again!!!!
 
Upvote 0
What's the range reference for HideRange?
Custom views aren't used much I guess, but you might try it (if the hidden columns are always the same). Don't need vba either for that :)
 
Upvote 0
Hi Xenou - Thanks for your idea. Unfortunately the columns that need hiding are different depending on the current date. Let me know if anything else comes to mind. Thanks so much! k
 
Upvote 0
My only experience with problems hiding (rows, usually) is because of re-calculation - i.e., when I apply a filter or remove one. So PA HS Teacher's idea is worth a try (turning off calculation while hiding the rows). There's nothing per se about your code that should take a long time but the devil is in the details.
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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