• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Hiding Columns

Hello Everyone,

I would like to do exactly what the title says, the issue is that i have already done so for each worksheet individually and I need to do this for the whole workbook as the structure is similar.

The question is How can I merge the following into a simple Sub??

Sub AmigosFood_Hide()
ActiveSheet.Range("A:A,F:F,H:I,L:M,W:W,Y:Y,AE:AG,AO:AP").EntireColumn.Hidden = True
End Sub

Sub BonAppetit_Hide()
ActiveSheet.Range("A:A,F:F,H:V,AB:AB,AH:AH,AN:AP,AX:AX").EntireColumn.Hidden = True
End Sub

Sub Bremner_Hide()
ActiveSheet.Range("A:A,F:F,H:I,T:T,Z:Z,AF:AH,AP:AR").EntireColumn.Hidden = True
End Sub

*Please note that those Subs are for worksheets separately so the worksheets' names are: AMIGOS FOOD, BON APPETIT, BREMNER,
 
Code:
Sub Hide()
Dim i As Integer
i = 1
For Sheet = i To 3
Sheets(i).Activate
ActiveSheet.Range("A:A,F:F,H:I,L:M,W:W,Y:Y,AE:AG,AO:AP").EntireColumn.Hidden = True
i = i + 1
Next
End Sub
Try this code.
 
The macro works in a sequential order if you sheet index are not in a sequential order it wont work ,but still you can use the same macro without loop but u will have to activate the sheet using the sheet names.
 
I don't think a loop is required I just need for instance hide several columns from several worksheets from a workbook, the there are different columns I want to hide in all the different worksheets, isn't there a macro that can do that If I provide you with the particular columns from each particular worksheet?

I got two worksheets: The first worksheet name is: DADDY RAYS
And the column ranges i want to hide are:
"A:A,F:F,H:N,Q:R,W:W,AC:AC,AI:AK,AI:AK,AT:AU"

I got two worksheets: The second worksheet name is: P&M
And the column ranges i want to hide are:
"A:A,F:F,H:J,T:T,Z:Z,AF:AH,AP:AQ"
 
Code:
Sub Hide()
Sheets("YourSheetname").Activate
ActiveSheet.Range("A:A,F:F,H:I,L:M,W:W,Y:Y,AE:AG,AO:AP").EntireColumn.Hidden = True
End Sub
Repeat this code for the sheets you want to hide in the same subroutine you don't have to write multiple subroutine to hide different sheets...
 
So then it would be:

Sub Hide()
Sheets("DADDY RAYS").Activate
ActiveSheet.Range("A:A,F:F,H:I,L:M,W:W,Y:Y,AE:AG,AO:AP").EntireColumn.Hidden = True

Sheets("P&M").Activate
ActiveSheet.Range("A:A,F:F,H:J,T:T,Z:Z,AF:AH,AP:AQ").EntireColumn.Hidden = True
End Sub

???? sorry for my ignorance i am totally new to VBA
 
You don't need to activate or select the sheets
so it can be simplified to:

Code:
Sub Hide()
Sheets("DADDY RAYS").Range("A:A,F:F,H:I,L:M,W:W,Y:Y,AE:AG,AO:AP").EntireColumn.Hidden = True
Sheets("P&M").Range("A:A,F:F,H:J,T:T,Z:Z,AF:AH,AP:AQ").EntireColumn.Hidden = True
End Sub
 
Back
Top