Excel 2016工作表-将工作表分配给工作表数组时出现错误91,用于工作簿中的每个工作表

htrmnn0y  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(76)

就像标题说的那样。当它到达指定的行时,我总是得到运行时错误91。
这可能是一些显而易见的东西,但我没有看到它。任何帮助都将不胜感激。这里是我的(缩写)代码:

Sub wat()
    Dim EHSBook As Workbook
    Dim Err as Boolean

    Dim sheetRecord As Worksheet
    Dim sheetAmList(7) As Worksheet
    Dim sheetPmList(7) As Worksheet
    Dim sheetAmListCnt As Integer
    Dim sheetPmListCnt As Integer

    sheetAmListCnt = 1
    sheetPmListCnt = 1

    Call OpenEhsFile(EHSBook, Err)
    If Err = True Then Exit Sub

    'get which sheets we need to total up
    For Each sheetRecord In EHSBook.Sheets
        If IsDate(sheetRecord.Cells(1, 3).Value) Then
            If Right(sheetRecord.name, 4) = "(AM)" Then
                ' next line throws error 91
                sheetAmList(sheetAmListCnt) = sheetRecord
                sheetAmListCnt = sheetAmListCnt + 1
            ElseIf Right(sheetRecord.name, 4) = "(PM)" Then
                ' next line throws error 91
                sheetPmList(sheetPmListCnt) = sheetRecord
                sheetPmListCnt = sheetPmListCnt + 1
            End If
        End If
    Next sheetRecord
End Sub

字符串
我试过了:我确保我正确地声明了我的数组,并且EHSBook被正确地分配了。
我期望:我期望代码添加记录(工作表),我需要处理的数组的基础上,如果它是上午或下午记录。
我得到:我得到一个弹出窗口,说运行时错误:'91':对象变量或与块变量未设置。
我保存到的数组是在同一个Sub中声明的,计数器表AmListCnt是在For Each之前分配的,For Each应该使用引用,所以不需要With.所以我很困惑。

f8rj6qna

f8rj6qna1#

返回数据结构对象

  • 要使数组基于一,可以动态定义它们,并使用To关键字调整它们的大小。
  • 如果变量声明为As Worksheet,则循环遍历Worksheets(而不是Sheets)集合。
  • 如果可以的话,您不想在不相关的单元格中查找(请参阅 Improvement 中的实现)。
  • 当将对象赋值给数组的元素时,需要使用Set关键字。
    快速修复(数组)
Sub watQF()
    
    ' Open the workbook.
    Dim EHSBook As Workbook ' : Set EHSBook = ThisWorkbook
    
    Call OpenEhsFile(EHSBook, Err)
    If Err = True Then Exit Sub

    Dim sheetRecord As Worksheet
    Dim sheetAmList() As Worksheet: ReDim sheetAmList(1 To 7) ' ***
    Dim sheetPmList() As Worksheet: ReDim sheetPmList(1 To 7) ' ***
    Dim sheetAmListCnt As Long ' ***
    Dim sheetPmListCnt As Long ' ***

    'get which sheets we need to total up
    For Each sheetRecord In EHSBook.Worksheets ' ***
        If IsDate(sheetRecord.Range("C1").Value) Then ' ***
            If Right(sheetRecord.Name, 4) = "(AM)" Then
                sheetAmListCnt = sheetAmListCnt + 1
                Set sheetAmList(sheetAmListCnt) = sheetRecord ' ***
            ElseIf Right(sheetRecord.Name, 4) = "(PM)" Then
                sheetPmListCnt = sheetPmListCnt + 1
                Set sheetPmList(sheetPmListCnt) = sheetRecord ' ***
            End If
        End If
    Next sheetRecord
    
    ' Print the sheet names by 'AM/PM'.
    
    Dim sheet As Variant ' *** !!!
    
    Debug.Print "Listing AM-sheets (Array)"
    For Each sheet In sheetAmList
        If Not sheet Is Nothing Then ' *** !!!
            Debug.Print sheet.Name
        End If
    Next sheet

    Debug.Print "Listing PM-sheets (Array)"
    For Each sheet In sheetAmList
        If Not sheet Is Nothing Then ' *** !!!
            Debug.Print sheet.Name
        End If
    Next sheet

End Sub

字符串
结果如下:
由于不区分大小写,列表很短。请参阅 * 改进 * 中的处理方式。

Listing AM-sheets (Array)
Mon(AM)
Listing PM-sheets (Array)


的数据

一个改进(集合)

为什么?为什么?

  • 您不需要预先确定大小,即您只需.Add
  • 不使用Set关键字。
  • 您可以使用适当的对象变量来循环遍历其项。
  • 你不需要检查一个元素是否包含一个对象:如果它不存在,它就不存在。
  • 请注意,使用 Dictionary 对象,“一个改进的集合”,可以增加更多的灵活性。
Sub wat()
    
    ' Open the workbook.
    Dim EHSBook As Workbook ' : Set EHSBook = ThisWorkbook

    Call OpenEhsFile(EHSBook, Err)
    If Err = True Then Exit Sub
    
    ' Define two collections to hold the worksheets to total up.
    Dim amList As Collection: Set amList = New Collection
    Dim pmList As Collection: Set pmList = New Collection
    
    Dim ws As Worksheet, wsName As String, Position As Long
    
    ' Add the worksheets whose names end with '(AM)' or '(PM)'
    ' to the associated collections i.e. 'amList' or 'pmList' respectively.
    For Each ws In EHSBook.Worksheets
        wsName = ws.Name
        Position = Len(wsName) - 3 ' -3 = -4 + 1
        Select Case True
            Case InStrRev(wsName, "(AM)", , vbTextCompare) = Position
                amList.Add ws
            Case InStrRev(wsName, "(PM)", , vbTextCompare) = Position
                pmList.Add ws
            Case Else
        End Select
    Next ws
     
    ' Print the sheet names by 'AM/PM'.
     
    Debug.Print "Listing AM-sheets (Collection)"
    For Each ws In amList
        Debug.Print vbTab & ws.Name
    Next ws
    
    Debug.Print "Listing PM-sheets (Collection)"
    For Each ws In pmList
        Debug.Print vbTab & ws.Name
    Next ws

End Sub


结果如下:

Listing AM-sheets (Collection)
    Mon(AM)
    Tue(am)
Listing PM-sheets (Collection)
    Mon(pm)
    Tue(Pm)

相关问题