VBA how to 'Save As' ?

Iceshade

Board Regular
Joined
May 22, 2017
Messages
104
Office Version
  1. 365
Platform
  1. Windows
Hoping I can get some help with this, quite new to VBA and loving what it can do !

I have this bit of code below which will save 2 sheets into 1 PDF:

VBA Code:
Sub CompileReport()
    Dim mySheets As Variant, sh
    Dim filename As Variant
    
        
    mySheets = Array("Print User", "Print Checklist")
    For Each sh In mySheets
        Sheets(sh).PageSetup.Orientation = xlLandscape
        Sheets(sh).PageSetup.FitToPagesWide = 1
                       
    Next

    Sheets(mySheets).Select
    
       
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:="H:\Desktop\Test" & ".pdf", _
       Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
        
  Sheets("Print User").Visible = False
  Sheets("Print Checklist").Visible = False
    
End Sub

My problem is in reference to the filename:="H:\Desktop\Test" & ".pdf" above. Can someone please teach me how I can, instead of defining the file and location, allow the user to select it themselves via a 'Save As' ? I have found a few guides on how to do this but I just can't seem to get it work.

Many thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
try this

VBA Code:
Sub CompileReportX()
    Dim mySheets As Variant, sh As Variant, fName As Variant
    fName = Application.GetSaveAsFilename(FileFilter:="Adobe PDF File_ (*.pdf), *.pdf")
        
    mySheets = Array("Print User", "Print Checklist")
    For Each sh In mySheets
        Sheets(sh).PageSetup.Orientation = xlLandscape
        Sheets(sh).PageSetup.FitToPagesWide = 1
    Next

    Sheets(mySheets).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=fName, IncludeDocProperties:=True, IgnorePrintAreas:=False
    Sheets("Print User").Visible = False
    Sheets("Print Checklist").Visible = False
End Sub
 
Upvote 0
Perfect
try this

VBA Code:
Sub CompileReportX()
    Dim mySheets As Variant, sh As Variant, fName As Variant
    fName = Application.GetSaveAsFilename(FileFilter:="Adobe PDF File_ (*.pdf), *.pdf")
       
    mySheets = Array("Print User", "Print Checklist")
    For Each sh In mySheets
        Sheets(sh).PageSetup.Orientation = xlLandscape
        Sheets(sh).PageSetup.FitToPagesWide = 1
    Next

    Sheets(mySheets).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=fName, IncludeDocProperties:=True, IgnorePrintAreas:=False
    Sheets("Print User").Visible = False
    Sheets("Print Checklist").Visible = False
End Sub
Perfect ! Really appreciate that ! Thank you @Yongle
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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