VBA Print and combine multiple sheets to PDF based on cell value

lpratt

New Member
Joined
Aug 6, 2010
Messages
22
i currently have the below code to print current sheet but what i need now is a code similar but will print multiple sheets based on cell values from a header page. EG. if a2 is yes then print sheet1 if a4 is yes then print sheet4..

thank you all


VBA Code:
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

On Error GoTo errHandler



Set wbA = ActiveWorkbook

Set wsA = ActiveSheet

strTime = wsA.Range("A1").Value _



'get active workbook folder, if saved

strPath = wbA.Path

If strPath = "" Then

strPath = Application.DefaultFilePath

End If

strPath = strPath & "\"



'replace spaces and periods in sheet name

strName = Replace(wsAName, " ", "")

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



'create default name for savng file

strFile = strName & "_" & strTime & ".pdf"

strPathFile = wsA.Range("A1").Value _



'use can enter name and

' select folder for file

myFile = Application.GetSaveAsFilename _

(InitialFileName:=strPathFile, _

FileFilter:="PDF Files (*.pdf), *.pdf", _

Title:="Select Folder and FileName to save")



'export to PDF if a folder was selected

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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I should add that the above code is exactly what i need ... but if header page A2 is Yes then print worksheet named "Sheet2" and if A4 is Yes then Print "Sheet1 & Sheet 4".

hope this makes sense
 
Upvote 0

Forum statistics

Threads
1,215,635
Messages
6,125,940
Members
449,275
Latest member
jacob_mcbride

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