如何将多个范围导出到单个PDF?

时间:2015-05-11 09:49:30

标签: excel vba pdf acrobat

我想将同一张表中的多个范围导出为单个pdf。

帐户1从A列开始,帐户2从C列开始,帐户3从E列开始,依此类推。每个帐户可以是n行,但我只希望pdf中的每个帐户最多50行,例如

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

在这种情况下,我希望我的pdf看起来像这样:

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

任何人都可以想到一个简洁的方法,不涉及将范围复制到新的工作表吗?

1 个答案:

答案 0 :(得分:0)

您必须制作代码才能选择合适的范围,但这样可行:

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

请注意,要使其正常工作,需要正确设置分页符。通过在您想要的位置设置分页符,这将打印选择中的页面。