Macro does not work when worksheet is hidden

Stormseed

Banned
Joined
Sep 18, 2006
Messages
3,274
Hi !

please tell me what am I doing wrong in this program ?

VBA Code:

Code:
Sub FilterCopy()
 
Application.ScreenUpdating = False

Sheets(Array("sec1", "sec2", "sec3", "sec4", "sec5")).Select
Cells.Select

Selection.ClearContents
 
Sheets("Raw Data").Activate

    [A1:m1000].AutoFilter Field:=1, Criteria1:="194A"
    [A1:m1000].Copy Sheets("sec1").[a1]
    
    [A1:m1000].AutoFilter Field:=1, Criteria1:="194C"
    [A1:m1000].Copy Sheets("sec2").[a1]
    
    [A1:m1000].AutoFilter Field:=1, Criteria1:="194I"
    [A1:m1000].Copy Sheets("sec3").[a1]
    
    [A1:m1000].AutoFilter Field:=1, Criteria1:="194J"
    [A1:m1000].Copy Sheets("sec4").[a1]
    
    [A1:m1000].AutoFilter Field:=1, Criteria1:="195"
    [A1:m1000].Copy Sheets("sec5").[a1]
    
    [A1:m1000].AutoFilter

Application.ScreenUpdating = True
 
End Sub

Execution of this code with all the worksheets visible works fine. However, if I make a worksheet (any one from sec1 to sec5) hidden or veryhidden then it throws a Debug Error "Select method of Sheets class failed" ??

Any idea why am I getting this and why am I supposed to hide these sheets before trigerring the macro ?

thanks a lot for your time
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You can't Select a hidden sheet, so avoid it:

Code:
Sub Test()
    Dim arrSheets As Variant
    Dim i As Integer
    Application.ScreenUpdating = False
    arrSheets = Array("sec1", "sec2", "sec3", "sec4", "sec5")
    For i = LBound(arrSheets) To UBound(arrSheets)
        Sheets(arrSheets(i)).Cells.ClearContents
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
ok, thanks for your help, sir.

I guess you gave me the code to clear the worksheets before the autofilter code is executed for fresh data to be copied, correct ?

Is the code below properly putup now ?

New code:
Code:
Sub Test()
    Dim arrSheets As Variant
    Dim i As Integer
    Application.ScreenUpdating = False
    arrSheets = Array("sec1", "sec2", "sec3", "sec4", "sec5")
    For i = LBound(arrSheets) To UBound(arrSheets)
        Sheets(arrSheets(i)).Cells.ClearContents
    Next i
 
    Sheets("Raw Data").Activate

    [A1:m1000].AutoFilter Field:=1, Criteria1:="194A"
    [A1:m1000].Copy Sheets("sec1").[a1]
    
    [A1:m1000].AutoFilter Field:=1, Criteria1:="194C"
    [A1:m1000].Copy Sheets("sec2").[a1]
    
    [A1:m1000].AutoFilter Field:=1, Criteria1:="194I"
    [A1:m1000].Copy Sheets("sec3").[a1]
    
    [A1:m1000].AutoFilter Field:=1, Criteria1:="194J"
    [A1:m1000].Copy Sheets("sec4").[a1]
    
    [A1:m1000].AutoFilter Field:=1, Criteria1:="195"
    [A1:m1000].Copy Sheets("sec5").[a1]
    
    [A1:m1000].AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Your problem was with hiding sheets, so I gave you some code that works. You can add whatever code you want to it.
 
Upvote 0

Forum statistics

Threads
1,214,874
Messages
6,122,034
Members
449,061
Latest member
TheRealJoaquin

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