Home > front end >  Modify created macro to print array of sheets instead of Activesheet
Modify created macro to print array of sheets instead of Activesheet

Time:12-12

I have a Macro that i have managed to put together (its rough, and im new to VBA but it does what i want - for the most part) It currently prints the active sheet to PDF and names it based on cell values. I want to adapt this to print 2 sheets into a single file (if its separate files, thats more than ok!) The cell Value naming bit can be changed at the top which i can do, but its calling for the export to pdf bit that im having an issue with.

I have tried reading up on the Activeworkbook functions but im not having much luck. I have tried calling for a sheet array, but it doesnt like the exportasfixedformat Type:= and im kind of new to that part too. It likes it in the original code, but not when i try and change the ActiveWorkbook.ActiveSheet, it spits it.

It would finalise my calculator :) Any help would be greatly appreciated.

Code:

Sub GetFilePath_Click()

 
Dim FileAndLocation As Variant
Dim strFilename As String

strFilename = Sheets("Leave Loading").Range("F13") & ", " & Sheets("Leave Loading").Range("F12") & " - " & Sheets("Leave Loading").Range("F14") & "- " & "Leave Loading" & ".pdf"

 
FileAndLocation = Application.GetSaveAsFilename _
(InitialFileName:=strPathLocation & strFilename, _
filefilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select a Location to Save")

ActiveWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFilename, OpenAfterPublish:=True

End Sub

Thank you in advance!

CodePudding user response:

Option Explicit
Sub GetFilePath_Click()
 
    Dim FileAndLocation As Variant
    Dim strFilename As String, strPathLocation As String
    
    strPathLocation = ""
    With Sheets("Leave Loading")
        strFilename = .Range("F13") & ", " & .Range("F12") & " - " _
                    & .Range("F14") & "- Leave Loading" & ".pdf"
    End With
     
    FileAndLocation = Application.GetSaveAsFilename _
    (InitialFileName:=strPathLocation & strFilename, _
    filefilter:="PDF Files (*.pdf), *.pdf", _
    Title:="Select a Location to Save")
    
    Sheets(Array("Sheet2", "Sheet4")).Select
    Sheets("Sheet2").Activate
    ActiveWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFilename, OpenAfterPublish:=True

End Sub
  • Related