Macro for saveas pdf for specific area in sheet of a workbook

RYALILAYR

New Member
Joined
Nov 5, 2017
Messages
2
Sir,

Good evening

am trying to write a macro for saveas a portion of excel worksheet in pdf format, but am unable to do the same since sintax errors coming, can any one can help me in this regard

one more thing is the file created should be name as content in cell "A1" of "sheet1"


Thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
How about this?

Code:
Sub SaveAsPDF()
Dim r As Range
Dim ws As Worksheet
Dim strPath As String

Set ws = Sheets("Sheet1")
With ws
    strPath = Environ("UserProfile") & "\Desktop\" & .Range("A1").Value & ".pdf" 'You can modify this to change the save location
    Set r = .Range("$G$13:$L$25") 'Change to Print Area you want to export
    .PageSetup.PrintArea = r.Address
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=True
End With
End Sub
 
Upvote 0
Sir,


Sub SaveAsPDF()
Dim r As Range
Dim ws As Worksheet
Dim strPath As String

Set ws = Sheets("Sheet1")
With ws
strPath = Environ("UserProfile") & "\Desktop" & .Range("A1").Value & ".pdf" 'You can modify this to change the save location
Set r = .Range("$G$13:$L$25") 'Change to Print Area you want to export
.PageSetup.PrintArea = r.Address
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End With
End Sub


while running the aforesaid code it showing as error in exportasfixedformat and highlightes the lines as set in bold above.... kindly help me resolve this issue


Thanks
 
Upvote 0
You need to put a valid file name, like "TestFile" in cell A1.

Then modify the code to suit your needs
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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