Save as pdf

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,392
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have code to save as a pdf but it defaults to the directory that the spreadsheet is located in. I want it to ask where it wants to save the file. Could someone help me with updating this vba code please?

Code:
Sub save_pdf()

    Dim currentSheet As Worksheet, fname As String
    fname = ActiveSheet.Range("G7").Value & ActiveSheet.Range("H5").Value    '" for " & ActiveSheet.Range("H5").Value
    
    With ThisWorkbook
        Set currentSheet = .ActiveSheet
        .ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:=ThisWorkbook.Path & "\" & fname & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    End With
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
.
Here is one method :

Code:
Option Explicit


Sub SaveOutput()
Dim folderChoice
Dim i As Integer
folderChoice = setupFolders()
ChDir (folderChoice)
    ActiveSheet.Select
     
    Call exportSheet(ActiveSheet.Range("G7").Value & ActiveSheet.Range("H5").Value)
End Sub

Function setupFolders()
Dim FolderName As String
    MsgBox ("Navigate to output directory...")
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        If .Show = 0 Then
            End
        End If
        FolderName = .SelectedItems(1)
    End With
    setupFolders = FolderName
End Function

Sub exportSheet(outputName)
ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=outputName & ".pdf"
End Sub
 
Last edited:
Upvote 0
UNTESTED....

Code:
Sub MM1()
Dim FileAndLocation As Variant, strFilename As String
strFilename = Range("G7").Value & Range("H5").Value
FileAndLocation = Application.GetSaveAsFilename(InitialFileName:=strFilename, filefilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
MsgBox "File saved to " & FileAndLocation
End Sub
 
Upvote 0
I want a dash between both cells. I thought I could use this code but it wouldn't work. What did I do wrong?

Code:
strFilename = Range("G7").Value & " - " Range("H5").Value
 
Upvote 0
.
Glad you have a solution and that I could assist.

Cheers.
 
Upvote 0
Just tried your solution again Michael and I realised that it doesn't save to pdf but I used the solution from Logit and that worked, thank you.
 
Upvote 0

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