Error when I try to hide Multiple Columns from Multiple Sheets

PVA0724

New Member
Joined
Apr 21, 2012
Messages
48
Hi

I started working with VBA a couple months ago, so I'm not an expert still in my learning curve. Now, I'm trying to hide multiple columns from the multiple worksheets (same columns for all of them), based on the case condition where 1 = Jan, meaning that it will hide everything except Jan information, 2 = Feb with the same result only showing that particular information. I thougt I had it done...but I'm getting Run time error 1004 Application Defined or Object Defined. I already checked the spelling and also what I got about the syntax...but at least to me everything looks ok...and keep me point me to the line below

Set c = Sheets("Master Control").Range(B2)
See the code below to understand where it is showed up.

Thanks in advance for any help, as I've stuck for almost two days on this. :confused:

Sub Hide()
Dim SheetArray As Variant
Dim rng As Range, c As Range
Dim I As Integer

SheetArray = Array("KPI Report Total", "KPI Report 60", "KPI Report 67", "KPI Report 69" _
, "KPI Report 72", "KPI Report 75", "KPI Report 76")

Sheets("Master Control").Activate

For I = 0 To UBound(SheetArray)

Set rng = ActiveWorkbook.Worksheets(SheetArray(I)).Range("B:BW")
Set c = Sheets("Master Control").Range(B2)

Select Case c
Case "1"
Columns("H:BW").Select
Selection.EntireColumn.Hidden = True
End Select
Next I
Sheets("Master Control").Activate
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Welcome to the board.

You're making this too complex - Select Case is used when the variable you're testing could have several values, but in this case, you're simply testing whether or not a single cell = 1. Also, you don't need to select sheets to perform actions on them.. The below code should work for you...


Code:
Sub Hide()
Dim SheetArray As Variant
Dim I As Integer

Application.ScreenUpdating = False

SheetArray = Array("KPI Report Total", "KPI Report 60", "KPI Report 67", "KPI Report 69" _
, "KPI Report 72", "KPI Report 75", "KPI Report 76")

For I = 0 To UBound(SheetArray)

    If Sheets("Master Control").Range("B2") = 1 Then Sheets(SheetArray(I)).Columns("H:BW").EntireColumn.Hidden = True

Next I

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Thanks for your prompt reply, but I showed only the example where the case = 1 then will perform the hide... but actually the case is up to 12 where each number represents a specific period and the hide will be different from that point would you be so kind to point how to use case and if I could do it without selecting the worksheets. Thanks again I have tried this one and it work but I also needed for several conditions. :cool:
 
Upvote 0
Thanks for your prompt reply, but I showed only the example where the case = 1 then will perform the hide... but actually the case is up to 12 where each number represents a specific period and the hide will be different from that point would you be so kind to point how to use case and if I could do it without selecting the worksheets. Thanks again I have tried this one and it work but I also needed for several conditions. :cool:

Something like this (untested)...

Code:
Sub Hide()
Dim SheetArray As Variant
Dim I As Integer
Dim c As Integer

Application.ScreenUpdating = False

SheetArray = Array("KPI Report Total", "KPI Report 60", "KPI Report 67", "KPI Report 69" _
, "KPI Report 72", "KPI Report 75", "KPI Report 76")

For I = 0 To UBound(SheetArray)

c = Sheets("Master Control").Range("B2")

Select Case c

    Case 1: Sheets(SheetArray(I)).Columns("H:BW").EntireColumn.Hidden = True
    Case 2: 'code to hide different sheets
    Case 3: 'code to hide different sheets
    Case 4 'etc
End Select

Next I

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Sub Hide()
Dim SheetArray As Variant
Dim rng As Range, c As Range
Dim I As Integer
SheetArray = Array("KPI Report Total", "KPI Report 60", "KPI Report 67", "KPI Report 69" _
, "KPI Report 72", "KPI Report 75", "KPI Report 76")

Set c = Sheets("Master Control").Range("B2")
Select Case c
Case "1"
For I = 0 To UBound(SheetArray)

Worksheets(SheetArray(I)).Columns("H:BW").EntireColumn.Hidden = True

Next I
End Select
Sheets("Master Control").Activate
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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