首页 文章

Excel VBA宏将一系列工作表保存到一个pdf,然后重复以用于不同系列的工作表

提问于
浏览
1

尝试将一组四个工作表打印到一个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

1 回答

  • 0

    您可以将每组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的方法 .

相关问题