Excel VBA to print multiple tab ranges to a PDF

shophoney

Active Member
Joined
Jun 16, 2014
Messages
281
Hi,

I'm trying to work with VBA to export multiple Excel tab ranges to a PDF. The range of data depends on the results.

The code below wont work.

Public Sub Monthend_All_Sheets_PDF()

'www.contextures.com
'for Excel 2010 and later
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim lastI As Long, lastQ As Long
Dim PDFranges As Range
On Error GoTo errHandler

With ActiveSheet
lastE = .Cells(.Rows.Count, "E").End(xlUp).Row
lastH = .Cells(.Rows.Count, "H").End(xlUp).Row
lastI = .Cells(.Rows.Count, "I").End(xlUp).Row
lastQ = .Cells(.Rows.Count, "Q").End(xlUp).Row

Set PDFranges = .Range("Monthend!A2:E" & lastE & ",Monthend!G2:H" & lastH & ",BO Sales Summary!A2:I" & lastI & ",BO Sales Summary!K2:Q" & lastQ)
End With

Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")

strPath = "D:\Dropbox\Shared_Files\dana_jordan\Reports"
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")

strFile = ActiveSheet.Range("A2").Value & " - " & strTime & ".pdf"
strPathFile = strPath & strFile

myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")

If myFile <> "False" Then
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If

exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hide the columns that are not to be included in the print range.
Hide columns 2, 4, 6 If your print range is from column A to H with the exception of columns B, D and F

BTW, use code tags as required.
 

Attachments

  • Use Code Tags MrExcel.JPG
    Use Code Tags MrExcel.JPG
    50.2 KB · Views: 14
Upvote 0
Hide the columns that are not to be included in the print range.
Hide columns 2, 4, 6 If your print range is from column A to H with the exception of columns B, D and F

BTW, use code tags as required.
Hi,

that didn't help. When I include more than one tab I get an error "Could not produce PDF"
 
Upvote 0
Re: When I include more than one tab I get an error
Please explain in detail.
As a matter of fact, explain in detail what you want to achieve.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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