0

I am struggling with hiding sheets.

I used two sets of code:

 Sub hidesheets()
 Sheets("Materials - Specifications").Visible = False
 Sheets("Fibre drop release sheet").Visible = False
 End Sub

And next

Sub RAtoPDF()
Call hidesheets
Dim sh As Worksheet, sh2 As Worksheet 
Dim ArraySheets() As String
Dim custom_name As String
Dim x As Variant
        
For Each sh In ActiveWorkbook.Worksheets
    If sh.Tab.ColorIndex = 33 Then
        'Sheets("Materials - Specifications").Visible = False
        ReDim Preserve ArraySheets(x)
        ArraySheets(x) = sh.Name
        x = x + 1
    End If
        
Next sh

Sheets(ArraySheets).Select

custom_name = "RA_" & ThisWorkbook.Name & ".pdf"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                                ThisWorkbook.Path & "\" & custom_name, _
                                Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                                IgnorePrintAreas:=False, OpenAfterPublish:=True
                                
Sheets("Frontsheet").Select
End Sub

I get:

Select method of sheets class failed

with debugger pointing to:

Sheets(ArraySheets).Select

I tried this line without an external macro, but the error is the same.

The XlVeryhidden option also is not working.

8
  • Are you trying to select sheets that are hidden? Commented Jun 19, 2020 at 15:15
  • When you cycle through the sheets it will include hidden sheets, but you can't select hidden sheets. Check your ArraySheets elements when the debugger halts on that line. View > Locals Window Commented Jun 19, 2020 at 15:17
  • I would like to deselect the sheets which I don't need to. I used the hidden option, because I though that the code will omit them. Commented Jun 19, 2020 at 15:22
  • 1
    You should just create a list of sheets you want to exclude and check against that when adding to your ArraySheets Commented Jun 19, 2020 at 15:23
  • 1
    What's the tab color for the two hidden sheets? You don't really say why your're hiding them or whether those are the sheets you want to exclude... Might help to be a bit more explicit in your question. Commented Jun 19, 2020 at 15:40

1 Answer 1

2

Select method of sheets class failed means that you select a hidden sheet for printing

If sh.Tab.ColorIndex = 33 And sh.Visible = True Then
Sign up to request clarification or add additional context in comments.

1 Comment

Yes! Perfect! This is exactly what I was looking for. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.