【问题标题】:Excel VBA Macro to save series of worksheets to one pdf then repeat for different series of worksheetsExcel VBA宏将一系列工作表保存到一个pdf然后重复不同系列的工作表
【发布时间】: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

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您可以将每组 4 个工作表合并为一个工作表以创建 PDF(例如,名为“A_PDF”的新工作表将包含从 A1 到 A4 的数据)。根据每个工作表的大小和结构,使用公式或宏来复制数据。然后修改您的 PDF 宏,使其一次只创建一个 PDF,并创建一个主宏以循环遍历 *_PDF 工作表:

    Sub MasterPDFCreator()
    
        Call PDFActiveSheetNoPrompt(Sheets("A_PDF"))
        Call PDFActiveSheetNoPrompt(Sheets("B_PDF"))
        Call PDFActiveSheetNoPrompt(Sheets("C_PDF"))
    
    End Sub
    
    Sub PDFActiveSheetNoPrompt(wsName as Worksheet)
    
    '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 = wsName
    
    '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
    
    exitHandler:
        Exit Sub
    errHandler:
        MsgBox "Could not create PDF file"
        Resume exitHandler
    
    End Sub
    

    也许不是最简洁的解决方案,但我还没有找到另一种将工作表合并为单个 PDF 的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 2020-06-11
      • 2014-01-28
      • 2014-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多