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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Tony Miall

Active Member
Joined
Oct 16, 2007
Messages
304
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
 

PA HS Teacher

Well-known Member
Joined
Jul 17, 2004
Messages
2,838
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
 

ringmaker

Board Regular
Joined
Oct 26, 2003
Messages
75
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!!!!
 

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

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 :)
 

ringmaker

Board Regular
Joined
Oct 26, 2003
Messages
75
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
 

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,133,717
Messages
5,660,469
Members
418,580
Latest member
IrishDave2137

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
Top