VBA to create a pdf, name it from a cell value and save to the users choice of location

jdhfch

Board Regular
Joined
Jan 25, 2018
Messages
79
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I am trying to create a VBA project that does the following:

Selects 2 named sheets - Sheet 1 & Sheet 2
Opens a Save As box for the user to select where they want to save the file
Creates a pdf file of the 2 selected sheets named as the same content in cell G3 and the current date,

I am sure this is easy to do, but it is blowing my mind! Any help would be appreciated,
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try:
VBA Code:
Sub CreatePDF()
    Application.ScreenUpdating = False
    Dim spath As String
    Sheets(Array("Sheet1", "Sheet2")).Copy
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count > 0 Then
            spath = .SelectedItems(1)
        End If
    End With
    ChDir spath
    With ActiveWorkbook
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveSheet.Range("G3") & Date _
            , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        .Close False
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub CreatePDF()
    Application.ScreenUpdating = False
    Dim spath As String
    Sheets(Array("Sheet1", "Sheet2")).Copy
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count > 0 Then
            spath = .SelectedItems(1)
        End If
    End With
    ChDir spath
    With ActiveWorkbook
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveSheet.Range("G3") & Date _
            , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        .Close False
    End With
    Application.ScreenUpdating = True
End Sub
Thank you, however I get a Runtime Error "1004" Application defined or object defined error?
 
Upvote 0
I tested the macro on some dummy sheets and it worked properly. Which line of code is highlighted when you click "Debug"?
 
Upvote 0
Thanks.. I have fixed the error. However, it closes the excel file after creating the pdf. Is there any way of stopping this, please?
 
Upvote 0
Delete this line of code:
VBA Code:
.Close False
 
Upvote 0
Solution

Forum statistics

Threads
1,224,818
Messages
6,181,151
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