How to: Hide a column on multipal sheets without looping.

K

Kent Prokopy

I want to be able to hide a column on all sheets of a workbook without having
to loop through each sheet. I have tried the following but it only hides the
column on sheet1

Sub Macro1()
' Macro1 Macro
' Macro recorded 8/20/2008 by Kent Prokopy
Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
Columns("D:D").EntireColumn.Hidden = True
End Sub

It makes no sense that I have to use a loop for this.
 
S

sbitaxi

I want to be able to hide a column on all sheets of a workbook without having
to loop through each sheet. I have tried the following but it only hides the
column on sheet1

Sub Macro1()
' Macro1 Macro
' Macro recorded 8/20/2008 by Kent Prokopy
  Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
  Columns("D:D").EntireColumn.Hidden = True
End Sub

It makes no sense that I have to use a loop for this.

Kent,

To my knowledge, VBA does not support range functions on multiple
sheet selections. You can change properties for multiple sheets at a
time using ActiveWorkbook.Sheets

You will have to loop through your worksheets to hide the column.

S
 
K

Kent Prokopy

I was afrade of this answer.

Thank you.

Kent,

To my knowledge, VBA does not support range functions on multiple
sheet selections. You can change properties for multiple sheets at a
time using ActiveWorkbook.Sheets

You will have to loop through your worksheets to hide the column.

S
 
J

Jim Thomlinson

Here is the code. Really no longer than yours. It has the added benefit of
not crashing if you change the sheet names or add / remove sheets...

Sub HideStuff()
dim wks as worksheet

for each wks in worksheets
wks.range("D:D").Entirecolumn.hidden = true
next wks
end sub
 
K

Kent Prokopy

Thank you for your time, But I was hoping to be able to do this without
looping through sheets.
 
R

Rick Rothstein \(MVP - VB\)

Why are you so set against looping through the sheets? Are you concerned
about having to select the different sheets? Look at Jim's code carefully...
notice that you do not have to select a sheet in order to do something on it
(you will remain on the same sheet you were on when you executed the code).

Rick
 
K

Kent Prokopy

My concern is speed. Looping is slow.

Rick Rothstein (MVP - VB) said:
Why are you so set against looping through the sheets? Are you concerned
about having to select the different sheets? Look at Jim's code carefully...
notice that you do not have to select a sheet in order to do something on it
(you will remain on the same sheet you were on when you executed the code).

Rick
 
R

Rick Rothstein \(MVP - VB\)

I'm afraid VBA requires you to loop the sheets... like 'sbitaxi', I know of
no way to go from sheet to sheet without using looping.

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top