Excel Macro - save all tabs (except specific ones) to single PDF file

Ns2003

New Member
Joined
Jan 23, 2017
Messages
2
Can you please help me with this? I would like to have a vba code that selects all tabs in the workbook, except for example "tab abc" and "tab def" and "tab ghi", then save to one single PDF.
Thanks in advance!

 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Welcome to MrExcel forums.

Try this code:
Code:
Public Sub Save_Sheets_As_PDF()

    Dim saveInFolder As String
    Dim replaceSelected As Boolean
    Dim ws As Worksheet
    Dim excludeSheets As String
    
    excludeSheets = "|abc|def|ghi|"        'The sheet names to exclude delimited by "|"
    
    saveInFolder = ThisWorkbook.Path
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
    
    With ThisWorkbook

        replaceSelected = True
        For Each ws In .Worksheets
            If InStr(excludeSheets, "|" & ws.Name & "|") = 0 Then
                ws.Select replaceSelected
                replaceSelected = False
            End If
        Next
            
        .ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveInFolder & "Sheets.pdf", _
            Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            
        .Worksheets(1).Select True
    
    End With
    
End Sub
 
Upvote 0
Thanks John!

I will try this, just have one question.

What directory is the pdf file being sent to right? Is there any way that I can specify a specific path to save it to?


Thanks!
 
Upvote 0
The PDF is saved in the same folder as the workbook. Change the saveInFolder string to a specific folder:
Code:
    saveInFolder = "C:\Path\to\folder"
 
Upvote 0

Forum statistics

Threads
1,216,759
Messages
6,132,551
Members
449,735
Latest member
Gary_M

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