【问题标题】:Using an array to select multiple sheets for printing使用数组选择多张纸进行打印
【发布时间】:2026-02-14 05:25:06
【问题描述】:

我一直在尝试使用多种技术来解决这个问题,但遇到了一些麻烦。

背景: 基本上,我希望根据位于“打印控制”工作表中的单元格的值将工作表导出为 pdf。值为“1”表示打印,“0”表示不打印。

正如您将在下面看到的,我有两个数组。首先包含“公司”列表,该值被替换到“P 1”工作表中的单元格 M1 中,以根据公司更改值。第二个数组包含需要打印的工作表列表。

本质上,我需要代码来检查是否应打印工作表,将其添加到数组(或选择它),对所有工作表重复,然后将数组(或选定的工作表)打印到 pdf 文件。完成后,我需要清空数组并为下一家公司执行相同的过程。

我在 If 语句中遇到问题。我不确定实现这一目标的最有效方法是什么。使用下面发布的代码,我得到一个下标超出范围的错误。我希望能提供一些意见来修复此代码,或提出更好的解决方法。

工作表名称可以在我尝试将每个工作表保存到 pagearray() 的 If 语句中看到。

谢谢,

这是我正在使用的:

Sub PrintCopies()
    Dim i As Integer
    Dim VList As Variant
    Dim pagearray() As String

    VList = Array("Company 1", "Company 2", "Company 3")
    For i = LBound(VList) To UBound(VList)
        ActiveWorkbook.Sheets("P 1").Range("M1") = VList(i)

        If ActiveWorkbook.Sheets("Print Control").Range("C2") = "1" Then
        pagearray(0) = "P 1"
        pagearray(1) = "P 2"
        End If
        If ActiveWorkbook.Sheets("Print Control").Range("D2") = "1" Then
        pagearray(2) = "PQS 1"
        pagearray(3) = "PQS 2"
        End If
        If ActiveWorkbook.Sheets("Print Control").Range("E2") = "1" Then
        pagearray(4) = "C 1"
        pagearray(5) = "C 2"
        End If
        If ActiveWorkbook.Sheets("Print Control").Range("F2") = "1" Then
        pagearray(6) = "A 1"
        pagearray(7) = "A 2"
        End If
        If ActiveWorkbook.Sheets("Print Control").Range("G2") = "1" Then
        pagearray(8) = "AQS 1"
        pagearray(9) = "AQS 2"
        End If
        If ActiveWorkbook.Sheets("Print Control").Range("H2") = "1" Then
        pagearray(10) = "L 1"
        pagearray(11) = "L 2"
        End If
        If ActiveWorkbook.Sheets("Print Control").Range("I2") = "1" Then
        pagearray(12) = "LQS 1"
        pagearray(13) = "LQS 2"
        End If
        If ActiveWorkbook.Sheets("Print Control").Range("J2") = "1" Then
        pagearray(14) = "Cess 1"
        pagearray(15) = "Cess 2"
        End If

ThisWorkbook.Sheets(Array(pagearray())).Select

Application.Calculate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "U:\Test File\" & ActiveWorkbook.Sheets("P1").Range("M1").Value, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
     IgnorePrintAreas:=False, OpenAfterPublish:=False
Application.Calculate
Application.Wait (Now + TimeValue("00:00:01"))

    Next
End Sub

【问题讨论】:

  • Dim pagearray() As String 更改为 Dim pagearray(16) As String 并查看您的下标超出范围错误是否消失。

标签: vba excel


【解决方案1】:

未经测试:

Sub PrintCopies()

    Dim wb As Workbook
    Dim i As Integer
    Dim VList As Variant
    Dim pages As String

    Set wb = ActiveWorkbook

    VList = Array("Company 1", "Company 2", "Company 3")

    For i = LBound(VList) To UBound(VList)
        ActiveWorkbook.Sheets("P 1").Range("M1") = VList(i)

        With wb.Sheets("Print Control")

            If .Range("C2") = "1" Then BuildString pages, "P 1|P 2"
            If .Range("D2") = "1" Then BuildString pages, "PQS 1|PQS 2"
            If .Range("E2") = "1" Then BuildString pages, "C 1|C 2"
            If .Range("F2") = "1" Then BuildString pages, "A 1|A 2"
            'etc etc

        End With

        If Len(pages) > 0 Then

            ThisWorkbook.Sheets(Split(pages, "|")).Select
            Application.Calculate
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                "U:\Test File\" & VList(i), _
                 Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                 IgnorePrintAreas:=False, OpenAfterPublish:=False
            Application.Calculate
            Application.Wait (Now + TimeValue("00:00:01"))

        End If

    Next i
End Sub

'ultility sub
Sub BuildString(ByRef str, addthis)
    str = str & IIf(Len(str) > 0, "|", "") & addthis
End Sub

【讨论】:

    【解决方案2】:

    对于任何寻找类似解决方案的人,这就是我最终使用的解决方案:

    ' Entryhook for the 'Print' button
    Sub PrintDocument()
        Call PrintSingle
    End Sub
    
    Sub PrintSingle()
        Dim worksheets As Collection
        Set worksheets = GetWorksheets()
    
        Set prop2 = ActiveWorkbook.Sheets("Prop 2")
    
        Dim strFileName As String
        strFileName =  'Enter Path Here
    
        Call PrintDoc(strFileName, worksheets)
    End Sub
    
    ' Entryhook for the 'Print All' button
    Sub PrintAll()
        Set wrksht = ActiveWorkbook.Sheets("Print Control")
        Set prop2 = ActiveWorkbook.Sheets("Prop 2")
        For Each company In wrksht.Range("A4:A54").cells
            prop2.Range("M1").Value = company
            Application.Calculate
            Call PrintSingle
        Next
    End Sub
    
    ' Prints a collection of worksheets as a PDF
    ' @param strFileName The name of the file
    ' @param worksheets The list of worksheets to print
    Sub PrintDoc(strFileName As String, worksheets As Collection)
        Sheets(collectionToArray(worksheets)).Select
        ActiveSheet.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=strFileName, _
           IgnorePrintAreas:=False
    End Sub
    
    ' Gets the worksheets that need to be present for a given worksheet
    ' @param company The ID of the company
    Function GetWorksheets() As Collection
        Dim switches As Collection
        Set switches = GetPrintSwitches()
    
        Dim wrksheets As Collection
        Set wrksheets = GetWorksheetMapping()
    
        Set wrksht = ActiveWorkbook.Sheets("Print Control")
        Set GetWorksheets = New Collection
        For Each pswitch In switches
            If wrksht.Range(pswitch) = "1" Then
                For Each doc In wrksheets.Item(pswitch)
                    GetWorksheets.Add doc
                Next
            End If
        Next
    End Function
    
    ' Gets a dictionary that maps a print switch to a list of worksheets to print
    Function GetWorksheetMapping() As Collection
        Set GetWorksheetMapping = New Collection
        GetWorksheetMapping.Add Item:=Array("P1", "P2"), Key:="B1"
        GetWorksheetMapping.Add Item:=Array("P2"), Key:="C1"
        GetWorksheetMapping.Add Item:=Array("PQS 1"), Key:="D1"
        GetWorksheetMapping.Add Item:=Array("PQS 2"), Key:="E1"
        GetWorksheetMapping.Add Item:=Array("C1"), Key:="F1"
    End Function
    
    ' Get a list of the cells to review for a print control
    Function GetPrintSwitches() As Collection
        Set GetPrintSwitches = New Collection
        GetPrintSwitches.Add "B1"
        GetPrintSwitches.Add "C1"
        GetPrintSwitches.Add "D1"
        GetPrintSwitches.Add "E1"
        GetPrintSwitches.Add "F1"
    End Function
    
    Function collectionToArray(c As Collection) As Variant()
        Dim a() As Variant: ReDim a(0 To c.Count - 1)
        Dim i As Integer
        For i = 1 To c.Count
            a(i - 1) = c.Item(i)
        Next
        collectionToArray = a
    End Function
    

    【讨论】: