Forum Discussion
I need help create a pdf with a button
I have tried to re-format that. Try this:
Private Sub CREATE_GARY_TEAMS_PDF_Click()
Dim FilePath As String
' Specify the output file path
FilePath = ThisWorkbook.Path & "\GARY NFL TEAMS SCHEDULE.pdf"
' Select the sheets you want to export as ONE PDF
Dim SheetArray As Variant
SheetArray = Array("STEELERS FOR FRIENDS", "CHARGERS FOR FRIENDS", "RAIDERS FOR FRIENDS", "COWBOYS FOR FRIENDS", "COWBOYS FOR FRIENDS", "COWBOYS FOR FRIENDS", "EAGLES FOR FRIENDS", "BEARS FOR FRIENDS", "49ERS FOR FRIENDS", "49ERS FOR FRIENDS", "CARDINALS FOR FRIENDS", "RAMS FOR FRIENDS")
' Ensure sheets exist before proceeding
Dim ws As Worksheet, i As Integer
For i = LBound(SheetArray) To UBound(SheetArray)
On Error Resume Next
Set ws = ThisWorkbook.Sheets(SheetArray(i))
If ws Is Nothing Then
MsgBox "Sheet '" & SheetArray(i) & "' not found!", vbExclamation, "Error"
Exit Sub
End If
On Error GoTo 0
Next i
' Export sheets as a single PDF
ThisWorkbook.Sheets(SheetArray).Select
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=FilePath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
' Deselect sheets to prevent selection issues
ThisWorkbook.Sheets(1).Select
' Adjust to a default sheet
MsgBox "PDF saved at: " & FilePath, vbInformation, "Export Complete"
End Sub
I'm getting this error message
Thanks You
- m_tarlerJun 14, 2025Bronze Contributor
well I have bad news and more bad news.
bad news 1:
That same code ran without error for me so I'm not sure why it is failing for you.
EDIT: is your macro in the workbook you are working on? just in case try changing each instance of "ThisWorkbook" with "ActiveWorkbook". Also, the loop just above that statement I think is supposed to find that as a problem and prompt you with a msgbox and exit but it is flawed. See the bottom for updated code.
bad news 2:
It doesn't seem to matter what I make that SheetArray it just keeps printing the entire workbook
bad news 3:
Even if I can figure out how to get it to print only the 'selected' sheets based on the SheetArray,
I see no reason why it would print those certain sheets 3x or 2x times. you can only 'select' a tab or not select the tab. you can't select it 2x or 3x times
GOOD news 1:
I DID figure out how to get excel to print only the SELECTED sheets. Replace the
ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
with
ThisWorkbook.Sheets(SheetArray(0)).ExportAsFixedFormat Type:=xlTypePDF, _
but the bad news is that as I predicted in #3 above it only prints that selected sheet 1x regardless of how many times it is in SheetArray
here is the updated code to try:
Private Sub CREATE_GARY_TEAMS_PDF_Click() Dim FilePath As String ' Specify the output file path FilePath = ActiveWorkbook.Path & "\GARY NFL TEAMS SCHEDULE.pdf" ' Select the sheets you want to export as ONE PDF Dim SheetArray As Variant SheetArray = Array("STEELERS FOR FRIENDS", "CHARGERS FOR FRIENDS", "RAIDERS FOR FRIENDS", "COWBOYS FOR FRIENDS", "COWBOYS FOR FRIENDS", "COWBOYS FOR FRIENDS", "EAGLES FOR FRIENDS", "BEARS FOR FRIENDS", "49ERS FOR FRIENDS", "49ERS FOR FRIENDS", "CARDINALS FOR FRIENDS", "RAMS FOR FRIENDS") ' Ensure sheets exist before proceeding Dim ws As Worksheet, i As Integer For i = LBound(SheetArray) To UBound(SheetArray) On Error GoTo sheetMissing Set ws = ActiveWorkbook.Sheets(SheetArray(i)) If ws Is Nothing Then sheetMissing: MsgBox "Sheet '" & SheetArray(i) & "' not found!", vbExclamation, "Error" Exit Sub End If On Error GoTo 0 Next i ' Export sheets as a single PDF ActiveWorkbook.Sheets(SheetArray).Select 'DoEvents ActiveWorkbook.Sheets(SheetArray(0)).ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=FilePath, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=True ' Deselect sheets to prevent selection issues ActiveWorkbook.Sheets(1).Select ' Adjust to a default sheet MsgBox "PDF saved at: " & FilePath, vbInformation, "Export Complete" End Sub