-1

I recorded a macro.

It works if all of the sheets are visible but when I hide a sheet it will not save to pdf.

This is the code.

Sub save_pdf() 
' 
' save_pdf Macro 
'

' 
Sheets(Array("TITLE", "CML", "CLUSTER", "ORS", "MOBILE", "YPS", "DEVICES", "PORTS")).Select 
Sheets("TITLE").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _ Sheets("MAIN").Range("customer_name") + " - Project Initiation_ Document.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True,_ IgnorePrintAreas:=False, OpenAfterPublish:=True 

Sheets("MAIN").Select

End Sub
Community
  • 1
  • 1
RHON
  • 1
  • 1
  • 2

2 Answers2

1

Something like this:

Sub ExportVisible()
    Dim shts, sht As Worksheet, s, i As Long


    shts = Array("TITLE", "CML", "CLUSTER", "ORS", "MOBILE", "YPS", "DEVICES", "PORTS")
    i = 0

    For Each s In shts
        Set sht = ActiveWorkbook.Sheets(s)
        If sht.Visible = xlSheetVisible Then
            i = i + 1
            sht.Select (i = 1) '"replace" parameter true when i=1
        End If
    Next s

    'Sheets("TITLE").Activate '<<EDIT: remove this

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                  Filename:=Sheets("MAIN").Range("customer_name").Value & _
                  " - Project Initiation_Document.pdf ", _
                  Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                  IgnorePrintAreas:=False, OpenAfterPublish:=True

End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
0

It's likely due to the use of .Select. We want to avoid using .Select wherever possible. Instead, just set a loop to go through each worksheet in your workbook.

Sub save_PDFs()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
    sht.ExportAsFixedFormat Type:=xlTypePDF, fileName:=Sheets("MAIN").Range("customer_name") + " - Project Initiation_ Document.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Next sht
End Sub

However, if you want only visible sheets, then do this:

Sub save_PDFs()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
    If sht.Visible = True Then sht.ExportAsFixedFormat Type:=xlTypePDF, fileName:=Sheets("MAIN").Range("customer_name") + " - Project Initiation_ Document.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Next sht
End Sub
BruceWayne
  • 22,923
  • 15
  • 65
  • 110
  • 1
    OP wants to export the sheets into a single PDF - the `Select` is required for that. – Tim Williams Sep 20 '16 at 18:18
  • @TimWilliams - Oh, really? That's the first (that I can recall) where `.Select` is required. How come I can't just add `sht.Select` and replace `sht.ExportAs...` with `ActiveSheet.ExportAs...`? That also throws an error for me. – BruceWayne Sep 20 '16 at 18:28
  • @TimWilliams - `Workbook.ExportAsFixedFormat` skips hidden sheets (at least in 2013). See [this answer](https://stackoverflow.com/a/36107539/4088852). – Comintern Sep 20 '16 at 23:52
  • @bruce hi bruce what it does is to save the main page where the checkboxes and other form located. actually i am trying to avoid to print the main page. thanks! – RHON Sep 22 '16 at 10:27
  • "@TimWilliams - Workbook.ExportAsFixedFormat skips hidden sheets (at least in 2013). See this answer. – Comintern" this link solved my problem, thanks Comintern!! – RHON Sep 22 '16 at 10:52