Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exporting Excel ranges on different tabs as a pdf in VBA

I’ve tried the below, but what it does it apply the range I specify in Sheet1 to ALL the other sheets. I’ve also tried using ActiveSheet.UsedRange.Select for each sheet, but that includes extraneous cells that I don’t want displayed in the PDF. Does anyone have any ideas? I’m running Office 365 on Windows.

Sub SaveRangesAsPDF()

   Sheets("Sheet1").Activate
   ActiveSheet.Range("A1:L42").Select
   Sheets("Sheet2").Activate
   ActiveSheet.Range("A1:G35").Select
  

   ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Select
   Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
      ThisWorkbook.Worksheets("Sheet3").Range("A2").Value, Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
      True
End Sub

Thanks so much!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is no option to print selections on multiple sheets. You can choose one of:

  1. Selection on a single sheet.
  2. Active sheet(s) – an active sheet or selected sheets considering the printing area or not.
  3. Entire workbook – all sheets considering the printing area or not.

The solution for you is to define the printing area on each sheet and print selected sheets.

Sheet1.PageSetup.PrintArea = "$A$1:$L$42"
Sheet2.PageSetup.PrintArea = "$A$1:$G$35"
Sheets(Array("Sheet1", "Sheet2")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
  ThisWorkbook.Worksheets("Sheet3").Range("A2").Value, Quality:=xlQualityStandard, _
  IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
  True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading