run action on multiple worksheets

cterjesen

Board Regular
Joined
Jun 23, 2003
Messages
119
I want to hide columns in multiple worksheets using this code:

Sheet3.Select

Dim rng As Range
Dim c As Range
Set rng = Range("E12:BR12")


For Each c In rng
If c.Value = 1 Then
c.EntireColumn.Hidden = False
Else
c.EntireColumn.Hidden = True
End If
Next c

How do I run this for multiple worksheets without repeating the code? I tried to use an array.....:

Worksheets(Array(3, 47, 48)).Select

.....but kept getting an error (run-time error '1004": Select method of Sheets class failed)

How do I cycle through all of the worksheets?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I want to hide columns in multiple worksheets using this code:
...
How do I run this for multiple worksheets without repeating the code? I tried to use an array.....:

...How do I cycle through all of the worksheets?

Not sure if you are wanting all sheets, or specific ones. Presuming specific sheets and using the sheets' indexes, maybe:
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> exa()<br><SPAN style="color:#00007F">Dim</SPAN> wks <SPAN style="color:#00007F">As</SPAN> Worksheet<br><SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> wks <SPAN style="color:#00007F">In</SPAN> ThisWorkbook.Worksheets(Array(1, 3, 4))<br>        <SPAN style="color:#00007F">For</SPAN> i = 70 <SPAN style="color:#00007F">To</SPAN> 5 <SPAN style="color:#00007F">Step</SPAN> -1<br>            <SPAN style="color:#00007F">If</SPAN> wks.Cells(12, i).Value = 1 <SPAN style="color:#00007F">Then</SPAN><br>                wks.Columns(i).Hidden = <SPAN style="color:#00007F">False</SPAN><br>            <SPAN style="color:#00007F">Else</SPAN><br>                wks.Columns(i).Hidden = <SPAN style="color:#00007F">True</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
Hope that helps,

Mark
 
Upvote 0
Thanks so much. Are you in Phoenix? Are you available for hire? I have some issues with a file I'm working on - looking for some affordable help.
 
Upvote 0
Thanks so much. Are you in Phoenix? Are you available for hire? I have some issues with a file I'm working on - looking for some affordable help.

I'm getting a type mismatch on this line:

If wks.Cells(12, i).Value = 1 Then

I am in Phoenix. I am afraid I am currently too busy to take on additional work.

Admittedly tired, but I'm not seeing what would cause a mis-match. Can you post the entire procedure as you currently have it?
 
Upvote 0

Forum statistics

Threads
1,224,588
Messages
6,179,743
Members
452,940
Latest member
rootytrip

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