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

时间:2018-05-09 18:30:36

标签: excel vba excel-vba

尝试将一组四个工作表打印到一个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 :(得分:0)

您可以将每组4个工作表合并为一个用于PDF创建的工作表(例如,名为&#34的新工作表; 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的方法。