I have this function that is called when I press a button in an Excel sheet. I have this code that works,(in code block below) but prints each sheet as a separate PDF file. I want to have all of these sheets in one singular PDF file.
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim savePath As String
' Loop through all worksheets in the workbook
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "IRP Records Review" Or ws.Name = "IRP SUMMARY" Or ws.Name = "IRP_1327-1328" Or ws.Name = "CORRESPONDENCE" Or ws.Name = "Opening Conference" Then
' change this to select each worksheet, then after selecting all the sheets print to pdf
' Define the path where you want to save the PDF
savePath = Application.DefaultFilePath & "" & ws.Name & ".pdf"
' Print each worksheet to PDF
ws.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=savePath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Else
End If
Next ws
Application.ScreenUpdating = True
'Add a msgbox to let user know it's done
MsgBox ("The files were saved here: " & Application.DefaultFilePath)
End Sub
I have tried following multiple different tutorials, one of which was like this:
Sheets(Array("IRP Records Review", "IRP SUMMARY", "IRP_1327-1328", "CORRESPONDENCE", "Opening Conference")).Select ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FolderPath & "IRP_Review", _ openafterpublish:=False, ignoreprintareas:=False
Which did not work. I kept getting the error “Runtime error ‘9’ Subscript out of range”. Other variations on the code above did not work either.
I would like to rewrite this function in the code block,but make it to where all the sheets are in the same pdf. If anyone has any advice on what I should do please let me know. Thank you!
1