VBA save excel worksheet with workbook name plus text as a PDF

Ed_B

New Member
Joined
Mar 28, 2018
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
This code only saves with the workbook name and not the text "Sample". What am I doing wrong? All answers appreciated Thanks, Ed

Sub SamplePDF()
Dim strFolder As String
Dim i As Long

'Find the position of the period in the file name
i = InStr(ActiveWorkbook.Name, ".")

Filename = Left(ActiveWorkbook.Name, i - 1) & " Sample "
Sheets(" Sample ").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Quality _
:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub




 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You need to specify the filename when saving.
Code:
Sub SamplePDF()
Dim strFolder As String
Dim i As Long

    'Find the position of the period in the file name
    i = InStr(ActiveWorkbook.Name, ".")

    Filename = Left(ActiveWorkbook.Name, i - 1) & " Sample "

    Sheets(" Sample ").Select

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FileName, Quality _
         :=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

End Sub
PS You'll probably also want to specify the folder the PDF is being save in, if you don't it'll get saved in what Excel considers the 'active' directory.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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