【发布时间】:2018-05-09 18:30:35
【问题描述】:
尝试将四个工作表的组打印到一个 PDF 的宏,直到打印工作簿中的所有工作表组。
我有一个包含多组工作表的工作簿。
工作表分为四个报告集:A1, A2, A3, A4, B1, B2, B3, B4...。
我想创建一个宏,允许我将工作表 A1-A4 打印到一个 PDF 中,然后对工作表 B1-B4, C1-C4... 重复此操作。
我想使用一系列单元格来命名 PDF。也许在工作表 A1 上,单元格 A1:A3(工作表 B1、单元格 A1:A3 等)。
最终结果将是一个文件夹,其中包含针对每组工作表 1-4 的一个 PDF。
我看到宏可以为一组工作表或选定的工作表执行此操作,但不知道如何让宏在完成第一组工作表后为另一组工作表重复该过程。
任何提示、提示、技巧、帮助或正确方向的点都是仁慈的、宽宏大量的。每次有人对数据进行一点更新时,尝试节省大量打印报告的时间......
下面的宏(不是我的)对选定的工作表做得很好,但不能一遍又一遍地处理一系列 4 个工作表。
Sub PDFActiveSheetNoPrompt()
'www.contextures.com
'for Excel 2010 and later
Dim wsa As Worksheet
Dim wba As Workbook
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wba = ActiveWorkbook
Set wsa = ActiveSheet
For Each wsa In ActiveWorkbook.Worksheets
'get active workbook folder, if saved
strPath = wba.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
strName = wsa.Range("A1").Value _
& " - " & wsa.Range("A2").Value _
& " - " & wsa.Range("A3").Value
'create default name for saving file
strFile = strName & ".pdf"
strPathFile = strPath & strFile
'export to PDF in current folder
wsa.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strPathFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
'MsgBox "PDF file has been created: " _
' & vbCrLf _
'& strPathFile
Next
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
【问题讨论】: