VBA how to 'Save As' ?

Iceshade

Board Regular
Joined
May 22, 2017
Messages
83
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
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
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
 

Iceshade

Board Regular
Joined
May 22, 2017
Messages
83
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
 

Watch MrExcel Video

Forum statistics

Threads
1,127,328
Messages
5,624,036
Members
416,007
Latest member
csf

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
Top