Combine Multiple Ranges in one pdf - excel

On the basis of a few posts I was able to make below script which prints several selected ranges to a pdf file. However, all ranges are printed on a seperate sheet.
Currently NewRng.Address="A1:G9,A13:G14,A18:G37". I think it might need to be "A1:G9;A13:G14;A18:G37" (seperated by ; instead of ,)(?)
Can someone explain how can I print the selected ranges on one sheet?
Thank you so much!
Script:
Sub CreatePDF_Selection1()
Dim rng1 As Range, rng2 As Range, rng3 As Range
Dim NewRng As Range
With ThisWorkbook.Sheets("Sheet1")
Set rng1 = .Range("A1:G9")
Set rng2 = .Range("A13:G14")
Set rng3 = .Range("A18:G37")
Set NewRng = .Range(rng1.Address & "," & rng2.Address & "," & rng3.Address)
Debug.Print NewRng.Address
Sheets("Sheet1").Activate
ActiveSheet.Range(NewRng.Address).Select
Sheets(Array("Sheet1")).Select
ThisWorkbook.Sheets(Array("Sheet1")).Select
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="U:\Sample Excel File Saved As PDF", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=True, _
From:=1, _
OpenAfterPublish:=True
End With
End Sub

Rather than select various ranges just hide the rows you don't want to print then print the entire range.
Option Explicit
Sub CreatePDF_Selection1()
Dim rng1 As Range
ThisWorkbook.Sheets("Sheet1").Activate
Set rng1 = Range("A1:G37")
Range("A10:A12").EntireRow.Hidden = True '*** Hide rows not to print ***
Range("A15:A17").EntireRow.Hidden = True
rng1.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="U:\Sample Excel File Saved As PDF", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=True, _
From:=1, _
OpenAfterPublish:=True
Rows("1:37").EntireRow.Hidden = False '*** Unhide hidden rows ***
End Sub 'CreatePDF_Selection1()
HTH
Edit: Attach test output.

I could not find a direct solution, so there is a work about here. A new worksheet will be added. The content will be copied there as a continuous range. The sheet will be exported as PDF, than the not needed sheet will be deleted.
Sub CreatePDF_Selection1()
Dim rng1 As Range, rng2 As Range, rng3 As Range
Dim NewRng As Range
Application.ScreenUpdating = False
With Sheet1
Set rng1 = .Range("A1:G9")
Set rng2 = .Range("A13:G14")
Set rng3 = .Range("A18:G37")
Set NewRng = Union(rng1, rng2, rng3)
End With
'Creating test values
rng1.Value = "Test 1"
rng2.Value = "Test 2"
rng3.Value = "Test 3"
NewRng.Copy
'adding a new sheet
Worksheets.Add after:=Sheet1
With ActiveSheet
.Paste
.ExportAsFixedFormat, _
Type:=xlTypePDF, _
Filename:="U:\Sample Excel File Saved As PDF", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=True, _
From:=1, _
OpenAfterPublish:=True
Application.DisplayAlerts = False
.Delete 'delete the unwanted worksheet
Application.DisplayAlerts = True
End With
Application.ScreenUpdating = True
End Sub

Export Non-Contiguous Range to PDF
This solution uses the Application.Union method to create the range to be exported. The range is then copied using the Range.Copy method to a newly added worksheet and exported from there. Then the newly added worksheet is deleted.
Option Explicit
Sub CreatePDF_Selection1()
Const FilePath As String = "U:\Sample Excel File Saved As PDF"
Const SheetName As String = "Sheet1"
Dim wb As Workbook
Set wb = ThisWorkbook ' The workbook containing this code.
' Define Copy Range.
With wb.Worksheets(SheetName)
Dim rng As Range
Set rng = Union(.Range("A1:G9"), .Range("A13:G14"), .Range("A18:G37"))
End With
' Copy Copy Range to new worksheet, export to PDF and delete new worksheet.
With Worksheets.Add
' This will copy values and formats.
rng.Copy .Range("A1")
.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=FilePath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=True
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
End With
End Sub

Related

Worksheet in Excel to output range objects data as PDF file, i want each range to be saved as separate pdf page?

I would like to create PDF from multiple ranges on different pages.
For example:
1st page of PDF has first 5 columns of active sheet and columns from 6-10. So, range A1:E10 && F1:J10
2nd page of PDF has first 5 columns of active sheet and columns 10-15. So, range from A1:E10 && K1:O10
3rd page has range from A1:E10 && P1:T10
And so on..
code
Private Sub cmdPrintJul_Click()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("YTD")
ws1.PageSetup.PrintArea = "A1:K48"
Set ws2 = Worksheets("July")
ws2.PageSetup.PrintArea = "A1:G45"
Worksheets(Array(ws1.Name, ws2.Name)).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="e:\saved\July2016.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
ws1.Activate
Range("A1").Select
End Sub
Does that accross multiple sheets, I need it on a single sheet, but concatenate multiple ranges together. Setting pages is probably gonna be done via PageSize, selecting the one that page breaks each combined range to its own page.
I tried with:
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("Celo leto 2019")
ws1.PageSetup.printArea = "B2:K39"
Set ws2 = Worksheets("Celo leto 2019")
ws2.PageSetup.printArea = "AQ2:BS39"
Worksheets(Array(ws1.Name, ws2.Name)).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strPathFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
ws1.Activate
Range("A1").Select
But it only exports ws2 range..
in sample two, it appears you are trying to print title columns from the same worksheet. I assume you want columns b thru k to repeat on each page. (sample one appears to be two worksheets) try adding column titles
Dim ws1 As Worksheet
Set ws1 = Worksheets("Celo leto 2019")
With ws1
.PageSetup.PrintTitleColumns = "$b:$k"
.PageSetup.PrintArea = "$aq2:$bs39"
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strPathFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End With
ws1.Activate
Range("A1").Select

VBA Excel saving as a PDF based on the sheet colours-Create array of sheets based on tab color

I would like to eligible my PDF document to save, although within the sheets with specified color.
How can I do that?
Sub SavecolorTabtoPDF()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If Tab Color = "Blue, Accent 1, Darker 25%" Then
'export as pdf
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:\" & Left(ws.Name, Len(ws.Name) - 2) & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End If
Next ws
End Sub
You want to loop through the sheets and create an array of sheets based on tab color.
This will create your array of sheets, then pdf them as one.
Sub MakeArraySheets()
Dim sh As Worksheet
Dim ArraySheets() As String
Dim x As Variant
For Each sh In ActiveWorkbook.Worksheets
If sh.Tab.ColorIndex = 55 Then
ReDim Preserve ArraySheets(x)
ArraySheets(x) = sh.Name
x = x + 1
End If
Next sh
Sheets(ArraySheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
dim wsheet as worksheet
For Each wsheet In Thisworkbook.Worksheets
if wsheet.tab.colorindex = 1 then
Worksheet.SaveAs(*parameters*)
end if
next wsheet
EDIT: The colorindex for your specified color is 55. Find out by filling a cell with that color and running "mgbox activecell.interior.colorindex"
EDIT2: Removed "interior" before colorindex

Excel VBA Macro select multiple ranges, even duplicate, and export to pdf

So, I managed to export specific range on my only sheet to PDF, no problems here.
Now I would like to create PDF from multiple ranges on different pages.
For example:
1st page of PDF has first 5 columns of active sheet and columns from 6-10. So, range A1:E10 && F1:J10
2nd page of PDF has first 5 columns of active sheet and columns 10-15. So, range from A1:E10 && K1:O10
3rd page has range from A1:E10 && P1:T10
And so on..
Snippet
Private Sub cmdPrintJul_Click()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("YTD")
ws1.PageSetup.PrintArea = "A1:K48"
Set ws2 = Worksheets("July")
ws2.PageSetup.PrintArea = "A1:G45"
Worksheets(Array(ws1.Name, ws2.Name)).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="e:\saved\July2016.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
ws1.Activate
Range("A1").Select
End Sub
Does that accross multiple sheets, I need it on a single sheet, but concatenate multiple ranges together.
Setting pages is probably gonna be done via PageSize, selecting the one that page breaks each combined range to its own page.
I tried with:
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("Celo leto 2019")
ws1.PageSetup.printArea = "B2:K39"
Set ws2 = Worksheets("Celo leto 2019")
ws2.PageSetup.printArea = "AQ2:BS39"
Worksheets(Array(ws1.Name, ws2.Name)).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strPathFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
ws1.Activate
Range("A1").Select
But it only exports ws2 range..

Using VBA to print to PDF existing macro

So i found this code online and was able to edit it to do what i want EXCEPT save as a PDF it currently set to only show me a print preview. Can someone explain how to edit this to save as a PDF with the File name being what ends up appearing in cell "A2"
Sub testme()
Dim TempWks As Worksheet
Dim wks As Worksheet
Dim myRng As Range
Dim myCell As Range
'change to match your worksheet name
Set wks = Worksheets("Sheet3")
Set TempWks = Worksheets.Add
wks.AutoFilterMode = False 'remove the arrows
'assumes headers only in row 1
wks.Columns(1).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=TempWks.Range("A1"), Unique:=True
With TempWks
Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
End With
With wks
For Each myCell In myRng.Cells
.UsedRange.AutoFilter Field:=1, Criteria1:=myCell.Value
Dim MyFileName As Variant
Dim MyfilePath As Variant
Dim rng As Range
Set wks = Worksheets("Sheet3")
Set rng = wks.Cells(2, 1)
MyfilePath = "C:\Users\mmunoz\Desktop\Teresa" 'this is whatever location you wish to save in
MyFileName = MyfilePath & "\" & rng.Value & ".pdf" 'You can do the below in just a couple of lines, but this is way more effective and stops issues later on
ChDir _
MyfilePath ' hold your save location
wks.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
MyFileName, Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False 'did you want to open the file after saving?
Next myCell
End With
Application.DisplayAlerts = False
TempWks.Delete
Application.DisplayAlerts = True
End Sub
I have a bunch of data that I need to filter to show only a client's lines of data and save that as a PDF to send to the client.
Thanks,
This is the gist of what you want. I've added comments to explain
Dim MyFileName As Variant
Dim MyfilePath As Variant
Dim rng As Range
Set rng = wks.Cells(2, 1)
MyfilePath = "N:\Desktop" 'this is whatever location you wish to save in
MyFileName = MyfilePath & "\" & rng.Value & ".pdf" 'You can do the below in just a couple of lines, but this is way more effective and stops issues later on
ChDir _
MyfilePath ' hold your save location
wks.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
MyFileName, Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True 'did you want to open the file after saving?
Option Explicit
Sub testme()
Dim TempWks As Worksheet
Dim wks As Worksheet
Dim myRng As Range
Dim myCell As Range
'change to match your worksheet name
Set wks = Worksheets("Sheet3")
Set TempWks = Worksheets.Add
wks.AutoFilterMode = False 'remove the arrows
'assumes headers only in row 1
wks.Columns(1).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=TempWks.Range("A1"), Unique:=True
With TempWks
Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
End With
With wks
For Each myCell In myRng.Cells
.UsedRange.AutoFilter Field:=1, Criteria1:=myCell.Value
Dim MyFileName As Variant
Dim MyfilePath As Variant
Dim rng As Range
Set rng = wks.Cells(2, 1)
MyfilePath = "C:\Users\mmunoz\Desktop\Teresa" 'File Location
MyFileName = MyfilePath & "\" & myCell.Value & ".pdf" 'File Name
ChDir _
MyfilePath
wks.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
MyFileName, Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next myCell
End With
Application.DisplayAlerts = False
TempWks.Delete
Application.DisplayAlerts = True
End Sub

Print Selected Sheets only... but Active Sheet keeps printing instead

I'm trying to loop through all active sheets and save them as separate PDFs.
dim ws as worksheet
dim path as string
...
For Each ws In ActiveWindow.SelectedSheets
ws.ExportAsFixedFormat _
xlTypePDF, _
Filename:=path & ws.Name, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenafterPublish:=False
Next
It kind of works:
A PDF file is created for each selected sheet in the correct folder... but...
The contents of those PDF files are all the same. It is the Active Sheet being printed each time but with a different filename.
How to fix this? Keep in mind, I only want selected sheets to print.
EDIT: I'm too new to upvote. Thanks for your answers!
You need to Select the sheet before printing out.
Just add the command ws.Select right before ws.ExportAsFixedFormat and it will work:
For Each ws In ActiveWindow.SelectedSheets
ws.Select '<-- the only thing you have to add.
ws.ExportAsFixedFormat _
xlTypePDF, _
Filename:=path & ws.Name, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenafterPublish:=False
Next
Apart for this:
Tip 1: to avoid seeing the sheets jumping over and over, you can add Application.ScreenUpdating = False before the loop starts and then setting it back to Application.ScreenUpdating = True at the end of the loop.
Tip 2: for user friendliness, you can get the currently active sheet at the beginning of the loop with Dim currentlySelectedSheet As Worksheet: Set currentlySelectedSheet = ActiveSheet and when the loop ends, you just select it back with currentlySelectedSheet.Select so that the user won't see anything change in their screen when running your macro.
Option Explicit
Sub Save_SelectedSheets_AsPdf()
Dim ws As Worksheet
Dim path As String
Dim actSheet As Worksheet
'...
Set actSheet = ActiveSheet
For Each ws In ActiveWindow.SelectedSheets
ws.Select
ws.ExportAsFixedFormat _
xlTypePDF, _
Filename:=path & ws.Name, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenafterPublish:=False
Next
actSheet.Select
End Sub
Sub Create_5_Sheets_and_name_them()
Dim iCt As Integer
For iCt = 2 To 5
Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Sh" & iCt
Range("A1").Value = ActiveSheet.Name
Next iCt
End Sub
you can specify it like this before you export it to pdf

Resources