How to export multiple ranges to a single PDF?

断了今生、忘了曾经 提交于 2021-01-28 04:44:03

问题


I want to export multiple ranges from the same sheet to a single pdf.

Account 1 starts in column A, account 2 starts in column C, account 3 starts in column E, and so on. Each account can be n rows long, but I only want a maximum of 50 rows for each account on a sheet within the pdf, e.g.

Account 1 = 100 rows long
Account 2 = 32 rows long
Account 3 = 56 rows long

In this case, I would want my pdf to look like this:

Sheet 1 = first 50 rows of account 1
Sheet 2 = next 50 rows of account 1
Sheet 3 = first 32 rows of account 2
Sheet 4 = first 50 rows of account 3
Sheet 5 = next 6 rows of account 3

Can anyone think of a neat way to do this that doesn't involve copying the ranges to a new sheet?


回答1:


You'll have to make code to select the proper range, but this will work:

Dim myfilename As String
Dim mysheet As Worksheet
Dim printRange As Range

myfilename = "C:\MyFolder\MyFile.pdf"
Set mysheet = ActiveWorkbook.Sheets("Sheet2")

'This is needed because even when you clear page breaks, it automatically creates a horizontal one around row 50
Set mysheet.HPageBreaks.Item(1).Location = mysheet.Range("A51")

'Make 2 vertical page breaks

mysheet.VPageBreaks.Add Before:=Range("C1")
mysheet.VPageBreaks.Add Before:=Range("E1")

'Insert code to set range here
Set printRange = mysheet.range("A1:F50,A51:B100,E51:F100")

printRange.ExportAsFixedFormat Type:=xlTypePDF _   
    , Filename:=myfilename

Note that for this to work properly, the page breaks need to be set properly. By setting the page breaks where you want them this will print the pages in the selection.



来源:https://stackoverflow.com/questions/30164466/how-to-export-multiple-ranges-to-a-single-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!