VBA Code help: Want to save unhide worksheets as pdf

Jasperlee93

New Member
Joined
Dec 6, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Sub ExportPDF()

Dim FDialog As FileDialog
Dim FPath As String
Dim Sht As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Set FDialog = Application.FileDialog(msoFileDialogFolderPicker)
FDialog.InitialFileName = ThisWorkbook.Path

If FDialog.Show = -1 Then
FPath = FDialog.SelectedItems(1)
End If

If FPath <> "" Then

For Each Sht In ThisWorkbook.Sheets
Sht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FPath _
& Application.PathSeparator & Sht.Name & ".pdf"
Next Sht

End If

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub


above vba code is what I am currently using.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi there

Try the below... From your question I am guessing you only want to export sheets that are not hidden...

VBA Code:
Sub ExportPDF()
    Dim FDialog     As FileDialog
    Dim FPath       As String
    Dim Sht         As Worksheet
    Application.ScreenUpdating = FALSE
    Application.DisplayAlerts = FALSE
    Set FDialog = Application.FileDialog(msoFileDialogFolderPicker)
    FDialog.InitialFileName = ThisWorkbook.Path
    If FDialog.Show = -1 Then
        FPath = FDialog.SelectedItems(1)
    End If
    If FPath <> "" Then
        For Each Sht In ThisWorkbook.Sheets
            If Sht.Visible = xlSheetVisible Then
                Sht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FPath _
                                      & Application.PathSeparator & Sht.Name & ".pdf"
            End If
        Next Sht
    End If
    Application.ScreenUpdating = TRUE
    Application.DisplayAlerts = TRUE
End Sub
 
Upvote 0
Solution
Glad we could assist and thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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