【问题标题】:Printing multiple excel worksheets to one pdf using VBA and getting Run-time error '9': subscript out of range when selecting tabs使用 VBA 将多个 excel 工作表打印到一个 pdf 并出现运行时错误“9”:选择选项卡时下标超出范围
【发布时间】:2017-06-19 14:04:06
【问题描述】:

我正在尝试创建一个 VBA 脚本,该脚本将选择多个工作表,然后将这些工作表导出为 PDF。我对编码仍然很陌生,但我对 PDF 部分的编码很好(让它在单个选项卡上工作)。我遇到问题的地方是选择多个工作表。我正在使用动态数组来查看工作表名称并确定是否选择它。一切正常,直到我到达选择工作表的部分。我得到一个运行时错误'9':下标超出范围。我在我的代码中放了几个 Debug.Prints 并且确实看到我的数组包含工作表名称。下面是我的代码。

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

【问题讨论】:

  • 半疯狂猜测:将 ReDim Sheetstoprint(0 To size) 更改为 ReDim Sheetstoprint(1 To size)
  • 你太棒了。那行得通。我花了 2 天时间,完全错过了。谢谢。
  • 这是一个我习惯于捕捉的错误,因为它经常出现在我自己的努力中。 ;-) 很高兴它有帮助。

标签: arrays vba excel worksheet-function


【解决方案1】:

我认为您在寻求解决方案时可能过于复杂。

这对我有用:

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

【讨论】:

    【解决方案2】:

    其他答案可能是更好的解决方法,但为了完整起见,我将解释您的代码中发生了什么。

    您将size 变量设置为等于工作簿中包含单词“segment”的工作表数。在我的测试用例中,我有 3 张(共 7 张)包含这个词。因此,size 变量设置为 3。当您将数组 Sheetstoprint 从 0 到 3 时,您总共允许保存 4 个工作表名称(索引 0、索引 1、索引 2 和索引 3 ) 尽管只有三张符合标准的工作表。或者更一般地说,如果您有 n 个名为“segment”的工作表,那么您正在为一个大小为 n+1 的数组标注尺寸。

    为了解决这个问题,我改变了

    ReDim Sheetstoprint(0 To size)
    

    ReDim Sheetstoprint(1 To size)
    

    然后为了补偿,我不得不在代码中重新排序 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
    

    收件人:

    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
    

    通过选择我期望的工作表似乎可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多