Home » excel » vba – Exporting Some Sheets from Excel Workbook to PDF

vba – Exporting Some Sheets from Excel Workbook to PDF

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am working on writing a VBA code to export some of the sheets in excel to same PDF. I have several chart sheets in my excel file each of which name ends with “(name)_Chart”.
I want to export all sheets with names ending wioth chart to one PDF file.
Here is the code I am trying to write.

Sub FindWS()
        'look if it at least contains part of the name
        Dim s As Worksheet
        Dim strPath As String

        strPath = ActiveWorkbook.Path & "\"

        For Each s In ThisWorkbook.Sheets
            If InStr(1, s.Name, Chart) Then
                s.Activate
                ActiveSheet.ExportAsFixedFormat xlTypePDF, strPath & s.Name & ".pdf"
                Exit Sub
            End If
        Next s
End Sub

This code is not limting export to only the chart sheets but exporting thy whole workbook. Can anyone help me with figurint out whats is missing in my code.

Thanks!

MODIFIED CODE:

Sub FindWS()
'look if it at least contains part of the name
Dim s As Worksheet
Dim strPath As String

strPath = ActiveWorkbook.Path & "\"

For Each s In ThisWorkbook.Worksheets
      If InStr(1, s.Name, "Chart") = 0 Then
          ' Hide the sheet so it is not exported as PDF
          s.Visible = False
             End If
Next s
          With ActiveWorkbook
          .ExportAsFixedFormat xlTypePDF, strPath & "TEST.pdf"
                End With

End Sub

How to&Answers:

I am surprised that your code is running in the first place 🙂 You should have actually got an error run time error '13', type mismatch

Sheets and Worksheets are two different things in Excel

The Worksheets collection is a collection of all the Worksheet objects in the specified or active workbook. Each Worksheet object represents a worksheet. Whereas the Sheets collection, on the other hand, consist of not only a collection of worksheets but also other types of sheets to include Chart sheets, Excel 4.0 macro sheets and Excel 5.0 dialog sheets.

So if you declare your object as Worksheet

Dim s As Worksheet

Then ensure that while looping you loop through the correct collection

For Each s In ThisWorkbook.Worksheets

and not

For Each s In ThisWorkbook.Sheets

else you will get a run time error '13', type mismatch

FOLLOWUP (Based on Comments)

@ Siddharth: 1. Yes, I want to export Chart sheets that ends with name “Chart”. 2. I want all those charts in one PDF and the name of the PDF should be the “original” file name. (I will have to save the final PDF files in different location so there will be no overlapping of files.) – datacentric

Option Explicit

Sub Sample()
    Dim ws As Object
    Dim strPath As String, OriginalName As String, Filename As String

    On Error GoTo Whoa

    '~~> Get activeworkbook path
    strPath = ActiveWorkbook.Path & "\"
    '~~> Get just the name without extension and path
    OriginalName = Left(ActiveWorkbook.Name, (InStrRev(ActiveWorkbook.Name, ".", -1, vbTextCompare) - 1))
    '~~> PDF File name
    Filename = strPath & OriginalName & ".pdf"

    '~~> Loop through Sheets Collesction
    For Each ws In ActiveWorkbook.Sheets
        '~~> Check if it is a Chart Sheet and also it ends in "Chart"
        If ws.Type = 3 And UCase(Right(Trim(ws.Name), 5)) = "CHART" Then
            ws.Visible = True
        Else
            ws.Visible = False
        End If
    Next ws

    '~~> Export to pdf
    ActiveWorkbook.ExportAsFixedFormat xlTypePDF, Filename

LetsContinue:
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

Answer:

This code will look through all the sheets. If the sheet name doesn’t match it will hide it. When it’s finished that it exports all visible sheets into one PDF. Make sure yuo don’t save the Excel file afterwards or the sheets will remain hidden.

Of course this code is not tested so if you have issues ask back (or try and resolve themself as you may learn something)

Sub FindWS()
    'look if it at least contains part of the name
    Dim s As Worksheet
    Dim strPath As String

    strPath = ActiveWorkbook.Path & "\"

    For Each s In ThisWorkbook.Sheets
        If InStr(1, s.Name, "Chart") = 0 Then
              ' Hide the sheet so it is not exported as PDF
              s.Visible = False 
        End If
    Next s

    ' Export all sheets as PDF         
    ActiveSheet.ExportAsFixedFormat xlTypePDF, strPath & "TEST.pdf"
End Sub