Excel VBA to save a sheet as a PDF and name it after the workbook name AND sheet name

jkalpus

New Member
Joined
Apr 17, 2004
Messages
46
Greetings. I've got an excel 2016 file, let's use the name Acme.xlsx. I need a macro to save a sheet (let's say it's called Sales) as a PDF and name that PDF after the workbook name AND the sheet name; e.g., ..... Acme_Sales.PDF.
I've got most of it down, and now need to add the sheet name to the PDF name.

From www.contextures.com I've got the following code: It works like a champ and now I want to concatenate the workbook name to the front of the PDF file.

Sub PDFActiveSheet()
'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 FileOnly As String ' JK test

On Error GoTo errHandler

'Set strwbA = ActiveWorkbook
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
Set wsWkSht = ActiveWorkbook
Set FileOnly = ActiveWorkbook ' JK test
strTime = Format(Now(), "yyyymmdd\_hhmm")

'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(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")

'create default name for savng file
strFile = FileOnly & " " & strName & "_" & strTime & ".pdf" 'Here's where I'm trying to enter the filename AND the sheet name
strPathFile = strPath & strFile

'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.
Try this.
Code:
    strFile = Split(wbA.Name, ".")(0) & "_" & wsA.Name & ".pdf"     'Here's where I'm trying to enter the filename AND the sheet name
 
Upvote 0
Try this.
Code:
    strFile = Split(wbA.Name, ".")(0) & "_" & wsA.Name & ".pdf"     'Here's where I'm trying to enter the filename AND the sheet name

Thanks, a million, Norie. This works like a champ!
If you have a moment, would you kindly explain the use of the "Split" command?
Again ... thank you!
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,154
Members
452,615
Latest member
bogeys2birdies

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