如何使用循环中的单个工作表创建PDF

时间:2016-08-03 13:20:38

标签: excel vba excel-vba pdf

现状

我的Excel工作簿包含 10到32个用作模板的工作表。它还包含1个名为“Report”的工作表。我目前遍历所有模板工作表并将信息放入报告工作表,然后使用该报告工作表创建PDF。

但是,这会为每个模板创建1个PDF,因为它会通过我的报告工作表。

每次我将信息发送到我的报告工作表时,都会变成PDF。问题是它每次填写我的报告工作表时都会生成PDF。我不会将模板导出为PDF格式。

代码

以下是每个模板生成1个PDF文件的代码

Sub CreatePDF()
Dim currentSerialNumber As String 'Worksheet name is the same as the serial number
Dim ws As Worksheet
Dim pdfFilePath As String

'Create a report for each serial number written in the Summary worksheet and export it to PDF
For i = 10 To Rows.Count 'Start at row #10
    If IsEmpty(Worksheets("Summary").Range("B" & i).Value) = False Then
        'Do work
        currentSerialNumber = Worksheets("Summary").Range("B" & i).Value 'Fetches the serial number
        pdfFilePath = "C:\" & currentSerialNumber & ".pdf" 'Ex: C:\1000.pdf
        Worksheets(currentSerialNumber).Activate 'Activate the template for this current serial number
        GenerateReport (currentSerialNumber) 'Put all the info from the template of this serial # into the report worksheet
        Set ws = Worksheets("Reports") 'Set ws object as the report worksheet
        ws.UsedRange.Select
        ws.ExportAsFixedFormat Type:=xlTypePDF, _
                               Filename:=pdfFilePath, _
                               Quality:=xlQualityStandard, _
                               IncludeDocProperties:=True, _
                               IgnorePrintAreas:=False, _
                               OpenAfterPublish:=False
    Else
        'No more serial numbers found, exit this loop
        Exit For
    End If
Next i
End Sub

目标

我想遍历每个模板,将信息发送到我的报告,将该报告信息作为页面添加到PDF文件中,然后重复直到不再有模板。

如果我打印所有模板,链接的类似问题也会起作用。但我实际上是将模板信息发送到更专业,更时尚的报告工作表。然后,报告就是我想要的PDF格式的报告。问题是,我只有一个报告工作表,由多个模板填充。

这可以在Excel中使用VBA吗?

1 个答案:

答案 0 :(得分:1)

我认为你可以循环处理这个问题。我的想法是做以下事情:

将数据复制到"报告"工作表,在新工作簿中创建该工作表的复制。这个新工作簿将包含每个"报告"工作表,然后您可以导出整个新工作簿

我还没有测试过,但让我们试试这样的事情:

Sub CreatePDF()
Dim currentSerialNumber As String 'Worksheet name is the same as the serial number
Dim ws As Worksheet
Dim pdfFilePath As String
Dim reports As Workbook

'## Add a new workbook an
Set reports = Workbooks.Add
ThisWorkbook.Activate
Do Until reports.Worksheets.Count = 1
    reports.Worksheets(reports.Worksheets.Count).Delete
Loop

'Create a report for each serial number written in the Summary worksheet and export it to PDF
For i = 10 To Rows.Count 'Start at row #10
    If IsEmpty(Worksheets("Summary").Range("B" & i).Value) = False Then
        'Do work
        currentSerialNumber = Worksheets("Summary").Range("B" & i).Value 'Fetches the serial number
        pdfFilePath = "C:\" & currentSerialNumber & ".pdf" 'Ex: C:\1000.pdf
        Worksheets(currentSerialNumber).Activate 'Activate the template for this current serial number
        GenerateReport (currentSerialNumber) 'Put all the info from the template of this serial # into the report worksheet
        Set ws = Worksheets("Reports") 'Set ws object as the report worksheet
        '## Copy the Reports sheet to the new workbook
        ws.Copy After:=reports.Worksheets(reports.Worksheets.Count)

    Else
        'No more serial numbers found, exit this loop
        Exit For
    End If
Next i

'## There is an empty worksheet in the Reports file, so we can remove it:
reports.Worksheets(1).Delete
'## select all sheets in reports:
reports.Worksheets.Select
'## Export the entire file as fixedformat:
reports.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                           Filename:=pdfFilePath, _
                           Quality:=xlQualityStandard, _
                           IncludeDocProperties:=True, _
                           IgnorePrintAreas:=False, _
                           OpenAfterPublish:=False
End Sub