如何在循环中使用单个工作表创buildPDF

现在的情况

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

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

每次我把信息发送到我的报告工作表,它变成了PDF。 这个问题是它每次生成一个PDF我的报告工作表被填写。我不会导出我的模板为PDF。


以下是每个模板生成一个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吗?

我想你可以循环处理这个问题。 我的想法是做到以下几点:

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

我还没有testing过,但是我们来试试这样的:

 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