简体   繁体   English

下标超出范围-运行时错误9

[英]Subscript out of range - runtime error 9

can you please advise why the below code does not select the visible sheets, but ends in a runtime error. 您能告诉我以下代码为什么不选择可见的工作表,而导致运行时错误的原因。 This is driving me crazy. 这真让我抓狂。 Thanks for any help. 谢谢你的帮助。

Sub SelectSheets1()

    Dim mySheet As Object
    Dim mysheetarray As String

    For Each mySheet In Sheets
        With mySheet
            If .Visible = True And mysheetarray = "" Then
                mysheetarray = "Array(""" & mySheet.Name
            ElseIf .Visible = True Then
                mysheetarray = mysheetarray & """, """ & mySheet.Name
            Else
            End If
        End With
    Next mySheet        

    mysheetarray = mysheetarray & """)"                
    Sheets(mysheetarray).Select

End Sub

Long story short - you are giving a string ( mysheetarray ) when it is expecting array. 长话短说-当期望数组时,您将给出一个字符串( mysheetarray )。 VBA likes to get what it expects. VBA喜欢达到期望。

Long story long - this is the way to select all visible sheets: 长话短说-这是选择所有可见工作表的方式:

Option Explicit

Sub SelectAllVisibleSheets()

    Dim varArray()          As Variant
    Dim lngCounter          As Long

    For lngCounter = 1 To Sheets.Count
        If Sheets(lngCounter).Visible Then
            ReDim Preserve varArray(lngCounter - 1)
            varArray(lngCounter - 1) = lngCounter
        End If
    Next lngCounter

    Sheets(varArray).Select

End Sub

You should define Dim mySheet As Object as Worksheet . 您应该将Dim mySheet As Object定义为Worksheet

Also, you can use an array of Sheet.Name s that are visible. 另外,您可以使用可见的Sheet.Name数组。

Code

Sub SelectSheets1()

    Dim mySheet As Worksheet
    Dim mysheetarray() As String
    Dim i As Long

    ReDim mysheetarray(Sheets.Count) '< init array to all existing worksheets, will optimize later
    i = 0
    For Each mySheet In Sheets
        If mySheet.Visible = xlSheetVisible Then
            mysheetarray(i) = mySheet.Name
            i = i + 1
        End If
    Next mySheet

    ReDim Preserve mysheetarray(0 To i - 1) '<-- optimize array size    
    Sheets(mysheetarray).Select

End Sub

I have tried to explain the Sheets a little, HTH. 我已经试着解释一下Sheets ,HTH。 Note: Sheets property is defined on Workbook and on Application objects, both works and returns the Sheets-Collection . 注意: Sheets属性是在WorkbookApplication对象上定义的,它们都可以工作并返回Sheets-Collection

Option Explicit

Sub SheetsDemo()
    ' All sheets
    Dim allSheets As Sheets
    Set allSheets = ActiveWorkbook.Sheets

    ' Filtered sheets by sheet name
    Dim firstTwoSheets As Sheets
    Set firstTwoSheets = allSheets.Item(Array("Sheet1", "Sheet2"))
    ' or simply: allSheets(Array("Sheet1", "Sheet2"))
    ' Array("Sheet1", "Sheet2") is function which returns Variant with strings

    ' So you simply need an array of sheet names which are visible
    Dim visibleSheetNames As String
    Dim sh As Variant ' Sheet class doesn't exist so we can use Object or Variant
    For Each sh In allSheets
        If sh.Visible Then _
            visibleSheetNames = visibleSheetNames & sh.Name & ","
    Next sh

    If Strings.Len(visibleSheetNames) > 0 Then
        ' We have some visible sheets so filter them out
        visibleSheetNames = Strings.Left(visibleSheetNames, Strings.Len(visibleSheetNames) - 1)
        Dim visibleSheets As Sheets
        Set visibleSheets = allSheets.Item(Strings.Split(visibleSheetNames, ","))

        visibleSheets.Select

    End If
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM