VBA to print from 2 different excel files?

misterno

Board Regular
Joined
Mar 16, 2009
Messages
78
I have this VBA that prints some sheets from one workbook to PDF. It works perfectly

But I need a VBA code that prints selected sheets from 2 different workbooks to one pdf file.

Here is the current code I have

Code:
Sub PrintPDF()
  Dim saveDIR As String
  
  Dim mydate As Date
  
  mydate = Sheets("New England Balances").Range("x2").Value
  
  On Error Resume Next
  
  saveDIR = "C:\Daily PDF Files\New England Gas Fundamentals"
   
  Sheets(Array("Chart1", "Chart2", "Chart3", "Chart4", "LDCs MA1", "LDCs MA2", "Elec Gen MA", "LDCs RI", "Elec Gen RI", "LDCs ME", "Elec Gen ME", "LDCs NH", "Elec Gen NH", "LDCs CT", "Elec Gen CT")).Select
   
  ChDir _
        "C:\Daily PDF Files"
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Daily PDF Files\" & "New England Gas Fundamentals_" & VBA.Format(mydate, "MMDDYYYY") & ".pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True
   
End Sub


And here is what I am testing. but this code only prints the worksheets from the second file. I want all sheets to be printed on the same one PDF

Code:
Sub PrintPDFtest()
  Dim saveDIR As String
  
  Dim mydate As Date
  
  mydate = Sheets("New England Balances").Range("x2").Value
  
  On Error Resume Next
  
  saveDIR = "C:\Daily PDF Files\New England Gas Fundamentals"
   
  Sheets(Array("Chart1", "Chart2", "Chart3", "Chart4", "LDCs MA1", "LDCs MA2", "Elec Gen MA", "LDCs RI", "Elec Gen RI", "LDCs ME", "Elec Gen ME", "LDCs NH", "Elec Gen NH", "LDCs CT", "Elec Gen CT")).Select
   
  Workbooks.Open Filename:= _
        "C:\test\Templates for Report content_test_v12AB1.xltm", UpdateLinks:=0
        
        Sheets(Array("Chart1", "Chart2", "Chart3", "Chart4", "Chart5", "Chart6", "Chart7", "Chart8")).Select
   
  ChDir _
        "C:\Daily PDF Files"
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Daily PDF Files\" & "New England Gas Fundamentals_" & VBA.Format(mydate, "MMDDYYYY") & ".pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True
    
End Sub
 
Last edited by a moderator:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,216,361
Messages
6,130,180
Members
449,563
Latest member
Suz0718

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