Save certain sheets as PDF

exl

Board Regular
Joined
Feb 4, 2010
Messages
153
Office Version
  1. 2019
Platform
  1. Windows
I have an excel workbook, where the end user does some selection, based on which the sheets which needs to be saved as a single PDF, are enlisted in Sheet 1, cell B10 like this (E1, E2, E3, E4...)

Now I have tried this macro below, but it does not work properly, the reasons are given below the code:

Code:
Sub pdff()

   Sheets("E1").Activate
   ActiveSheet.UsedRange.Select
   Sheets("E2").Activate
   ActiveSheet.UsedRange.Select
   Sheets("E3").Activate
   ActiveSheet.UsedRange.Select

   ThisWorkbook.Sheets(Array("E1", "E2","E3")).Select
   Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
      "C:\Users\Damon\Desktop\pdfmaker.pdf", Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
      True
End Sub

The problems with the code:
  • After the code runs, it groups the sheets together, which has to be ungrouped manually.
  • The code saves the areas outside of the set print area (which does not happen if I save each sheet manually as PDF)
  • The code is not dynamic, it does not pick up the sheet names from cell B10

Request help from the forum. Thanks
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
  • The ungrouping can be fixed by adding a sheet selection at the end
  • Before printing setup your print area for each sheet. When you use the UsedRange method, you could get extra whitespace in your print range
  • To make the selection dynamic, split the string for B2 into an array that can be used to setup the sheet selection

Code:
Sub pdff()
Dim ArrRng() As String
Dim sNames As String
Dim i As Integer
Dim Path As String

sNames = ThisWorkbook.Sheets(1).[B2]
ArrRng = Split(sNames, ",")
ThisWorkbook.Sheets(ArrRng(0)).Select

For i = LBound(ArrRng) To UBound(ArrRng)
    ThisWorkbook.Sheets(Trim(ArrRng(i))).Select False
Next i
ThisWorkbook.Sheets(ArrRng(0)).Activate

Path = CreateObject("WScript.Shell").specialfolders("Desktop")

ThisWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=Path & "\pdfmaker.pdf", _
    OpenAfterPublish:=True
ThisWorkbook.Sheets(1).Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,072
Messages
6,128,631
Members
449,460
Latest member
jgharbawi

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