Save As .pdf VBA Returns Error on Hidden Sheets

davidbrent

New Member
Joined
Feb 25, 2016
Messages
6
Hello All,

I have a code that works great at saving open worksheets as separate .pdf but when i hide i sheet it returns an invalid call. I'm sure its a very simple fix but well beyond my beginner level unfortunately

Sub ExportAsPDF()

Dim Folder_Path As String

With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select Folder path"
If .Show = -1 Then Folder_Path = .SelectedItems(1)
End With

If Folder_Path = "" Then Exit Sub

Dim sh As Worksheet

For Each sh In ActiveWorkbook.Worksheets
sh.ExportAsFixedFormat xlTypePDF, Folder_Path & Application.PathSeparator & sh.Name & ".pdf"
Next

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi davidbrent,

check if the worksheet is visible?

VBA Code:
Sub ExportAsPDF()

Dim strFoldPath As String
Dim wksSheet As Worksheet

With Application.FileDialog(msoFileDialogFolderPicker)
  .Title = "Select Folder path"
  If .Show <> -1 Then Exit Sub
  strFoldPath = .SelectedItems(1)
End With

For Each wksSheet In ActiveWorkbook.Worksheets
  If wksSheet.Visible = xlSheetVisible Then
    wksSheet.ExportAsFixedFormat xlTypePDF, strFoldPath & Application.PathSeparator & wksSheet.Name & ".pdf"
  End If
Next wksSheet

End Sub

Ciao,
Holger
 
Upvote 0
Solution
Hi davidbrent,

check if the worksheet is visible?

VBA Code:
Sub ExportAsPDF()

Dim strFoldPath As String
Dim wksSheet As Worksheet

With Application.FileDialog(msoFileDialogFolderPicker)
  .Title = "Select Folder path"
  If .Show <> -1 Then Exit Sub
  strFoldPath = .SelectedItems(1)
End With

For Each wksSheet In ActiveWorkbook.Worksheets
  If wksSheet.Visible = xlSheetVisible Then
    wksSheet.ExportAsFixedFormat xlTypePDF, strFoldPath & Application.PathSeparator & wksSheet.Name & ".pdf"
  End If
Next wksSheet

End Sub

Ciao,
Holger
Thank you so much for this!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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