简体   繁体   English

使用VBA将多个Excel工作表打印到一个pdf并获得运行时错误'9':选择选项卡时下标超出范围

[英]Printing multiple excel worksheets to one pdf using VBA and getting Run-time error '9': subscript out of range when selecting tabs

I am trying to create a VBA script that will select multiple worksheets and then export those worksheets to PDF. 我正在尝试创建一个VBA脚本,该脚本将选择多个工作表,然后将这些工作表导出为PDF。 I am still pretty new to coding, but I am fine with the coding for the PDF portion (got it to work on a single tab). 我对编码还是很陌生,但是我对PDF部分的编码很好(让它在单个选项卡上工作)。 Where I am having trouble is selecting multiple worksheets. 我遇到麻烦的地方是选择多个工作表。 I am using a dynamic array to look at the worksheet name and determine to select it or not. 我正在使用动态数组来查看工作表名称并确定是否选择它。 Every thing works fine until I get to the part where I select the worksheets. 一切正常,直到我到达选择工作表的那一部分。 I get a Run-time error'9': Subscript out of range. 我收到运行时错误“ 9”:下标超出范围。 I have put several Debug.Prints in my code and do see that my array contains worksheet names. 我在代码中放入了多个Debug.Prints,并确实看到我的数组包含工作表名称。 Below is my code. 下面是我的代码。

Sub pdf_Print()

Dim c As Integer
Dim d As Integer
Dim size As Integer
Dim i As Integer
Dim s As Integer
Dim wba As Workbook
Dim wa As Worksheet
Dim b As Integer


Set wba = ActiveWorkbook

'Debug prints active workbook name for Debugging
Debug.Print wba.Name

'Gets number of tabs to print
size = GetPrintTabs(wba)

'Setup Array for tabs to print
Dim Sheetstoprint() As Variant
ReDim Sheetstoprint(0 To size)
'Debug print Array size
Debug.Print size

'Stores tab names in Array
For Each wa In ActiveWorkbook.Worksheets
   If (wa.Name Like "*segment*" And wa.Visible = True) Then

   Sheetstoprint(i) = wa.Name
   i = i + 1
   Debug.Print Sheetstoprint(i)
 End If
Next wa


'Debug to ensure show which tabs are in Array
For b = LBound(Sheetstoprint) To UBound(Sheetstoprint)
Debug.Print Sheetstoprint(b)
Next

'Select sheets in Array
ActiveWorkbook.Worksheets(Sheetstoprint).Select

End Sub

Public Function GetPrintTabs(awb As Workbook) As Integer
Dim wsa As Worksheet
Dim i As Integer
For Each wsa In awb.Worksheets
   If (wsa.Name Like "*segment*" And wsa.Visible = True) Then
   i = i + 1
   End If
   Next wsa
GetPrintTabs = i
Debug.Print "size =" & i
End Function

I think you may have over-complicated things in your quest of not quite getting the solution. 我想您可能会因为过于复杂而无法获得解决方案。

This works for me: 这对我有用:

Sub SelectMultipleSheets()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

    Dim SheetsToPrint() As Variant
    Dim i As Integer

    If (ws.Name Like "*segment*" And ws.Visible = True) Then

        ReDim Preserve SheetsToPrint(i)
        SheetsToPrint(i) = ws.Name
        i = i + 1

    End If

Next

Worksheets(SheetsToPrint).Select


End Sub

The other answers are probably a better way to go about it, but for the sake of completeness I'll explain what's happening in your code. 其他答案可能是解决问题的更好方法,但是为了完整起见,我将解释代码中发生的事情。

You set the size variable to equal the number of sheets that contain the word 'segment' in the workbook. 您将size变量设置为等于工作簿中包含单词“ segment”的工作表数。 In my test case, I had 3 sheets (of 7 total) that contained the word. 在我的测试用例中,我有3张纸(共7张)包含单词。 The size variable, therefore, gets set to 3. When you dimension the array Sheetstoprint from 0 to 3 you are allowing a total of 4 sheet names to be held (index 0, index 1, index 2, and index 3) despite only having three sheets that match the criterion. 因此,将size变量设置为3。将数组Sheetstoprint尺寸从0设置为3时,尽管只具有4个工作表名称(索引0,索引1,索引2和索引3),但仍允许保留它们。符合条件的三张纸。 Or more generally if you have n sheets named like 'segment' you are dimensioning an array of size n+1. 或更笼统地说,如果您有n个像“ segment”一样的工作表,则您正在确定尺寸为n + 1的数组的尺寸。

To correct this I changed 为了纠正这一点,我改变了

ReDim Sheetstoprint(0 To size)

To

ReDim Sheetstoprint(1 To size)

And then to compensate I had to re-order the i variable farther down in the code, so I changed this: 为了补偿,我不得不在代码中更远的位置对i变量进行重新排序,因此我对此进行了更改:

For Each wa In ActiveWorkbook.Worksheets
   If (wa.Name Like "*segment*" And wa.Visible = True) Then

   Sheetstoprint(i) = wa.Name
   i = i + 1
   Debug.Print Sheetstoprint(i)
 End If
Next wa

To: 至:

For Each wa In ActiveWorkbook.Worksheets
   If (wa.Name Like "*segment*" And wa.Visible = True) Then
   i = i + 1
   Sheetstoprint(i) = wa.Name
   Debug.Print Sheetstoprint(i)
 End If
Next wa

And it seems to work fine by selecting the sheets I would expect. 通过选择我期望的工作表,它似乎可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在VBA中打印Long to Cells数组并获取下标超出范围错误 - Printing Array of Long to Cells in VBA and getting subscript out of range error 运行时错误9:使用字符串数组时下标超出范围 - Run-time error 9: Subscript out of range while working with string arrays 在Excel 2010 VBA中使用变体数组的运行时错误 - Run-time error using variant array in Excel 2010 VBA VBA-下标超出范围错误 - VBA - Subscript out of range error Excel VBA: Why am I getting a run-time error 438 when adding custom class object into custom class array? - Excel VBA: Why am I getting a run-time error 438 when adding custom class object into custom class array? 遍历数组Excel VBA时下标超出范围 - Subscript out of range when looping through array excel VBA VBA将数组写入范围,导致运行时错误“ 7”:内存不足 - VBA Writing Array to Range Causing Run-Time Error '7': Out Of Memory 将数组设置为范围时下标超出范围VBA错误 - Subscript out of range VBA error when setting an array equal to a range 在VBA-Excel中将1个Sub分为2个Sub时出现“下标超出范围”错误 - “Subscript out of range” error when I divide 1 Sub into 2 Sub's in VBA-Excel 将值分配给数组时,Excel VBA下标超出范围错误 - Excel VBA Subscript out of range error in array when assigning values to array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM