如何修复运行时错误“ 91”:对象变量或未设置块变量

时间:2019-10-24 16:46:50

标签: excel vba

我正在建立一个宏,该宏根据Excel表格中的交易ID单元格不为空的方式打开不同的网站。它将创建4个对象并将这些对象存储在一个集合中。我选择4作为任意数字,因为我永远都不需要超过2,但是为了以防万一,我创建了更多数字。

宏在excel工作表中循环,并根据需要选择尽可能多的交易ID(交易ID附加到url上以转到不同的站点),但是当我运行宏时,出现错误消息“对象变量或下方突出显示的行上“未设置块变量”。请帮忙,因为我对VBA的了解不是很强,而且我正在学习使工作中的某些事情自动化的绳索

我是VBA的新手,所以我不知道要怎么解决。在此问题上的任何帮助将不胜感激。谢谢enter image description here

Sub TransactionMatching()
    Dim first_day As String

    Dim ieapp As Object
    Dim ieapp2 As Object
    Dim ieapp3 As Object
    Dim ieapp4 As Object


    ' collection to hold deal names
    Dim dealnameArray As New Collection
    ' collection to hold deal IDs
    Dim dealIDArray As New Collection
    ' collection to hold required ieapp objects
    Dim totalDealObjectArray As New Collection

    ' add all ieapp objects to the collection
    totalDealObjectArray.Add ieapp
    totalDealObjectArray.Add ieapp2
    totalDealObjectArray.Add ieapp3
    totalDealObjectArray.Add ieapp4

    Windows("transaction_matching.xlsm").Activate

    ' loop through each row in the excel sheet and add the deal names and deal IDs...
    ' ...with check marks nect to them to their respective collections
    For i = 5 To 51
        If IsEmpty(Range("C" & i).Value) = False Then
            dealnameArray.Add (Range("A" & i).Value)
            dealIDArray.Add (Range("B" & i).Value)
        End If
    Next

    'get the required number of objects from the ieapp object collection
    For i = 1 To dealnameArray.Count - 1
        ' set each object in ieapp object collection to a new internet explorer object
        Set totalDealObjectArray(i) = New InternetExplorerMedium
        totalDealObjectArray(i).Visible = True

        ' define the last business day
        lastDay = DateSerial(Year(Date), Month(Date), 0)

        ' define the first day of the previous month
        first_day = lastDay - Day(lastDay) + 1

        With totalDealObjectArray(i)
            .navigate "http://website" & dealIDArray(i)
            Application.DisplayFullScreen = True
            Call busy((totalDealObjectArray(i)))
            Call DoThings((totalDealObjectArray(i)))
        End With
    Next

    Application.WindowState = xlNormal
    Application.WindowState = xlMaximized

End Sub

2 个答案:

答案 0 :(得分:3)

VBA中的集合使用.Add.Remove添加和删除项目。通过其他代码-How to change value of an item of a collection

可以更改集合中项目的值

collection.Item(N)显示该值,但不更改。关于代码,您可以添加新对象,然后将其设置为:

Sub TransactionMatching()

    Dim i As Long
    Dim totalDealObject As New Collection

    totalDealObject.Add New InternetExplorerMedium
    totalDealObject.Add New InternetExplorerMedium
    totalDealObject.Add New InternetExplorerMedium
    totalDealObject.Add New InternetExplorerMedium

    For i = 1 To 4
        Debug.Print totalDealObject.Item(i).FullName
    Next

End Sub

如果任务是通过循环将项目添加到集合中,则可以执行以下操作,在集合的每个第二位置添加InternetExplorerMedium:

Sub TransactionMatching()

    Dim i As Long
    Dim totalDealObject As New Collection

    For i = 1 To 10
        If i Mod 2 = 0 Then
            totalDealObject.Add New InternetExplorerMedium
        Else
            totalDealObject.Add i
        End If
    Next

End Sub

enter image description here

答案 1 :(得分:2)

Vityata正确识别了问题:

  

collection.Item(N)显示值,但不会更改

换句话说,=赋值运算符不会违反指定集合索引处的空对象引用;左侧分配目标是检索到的对象的默认成员,并且由于检索到的对象是Nothing,因此隐式默认成员调用就是为什么会出现错误91的原因,因为对Nothing的任何成员调用(无论是否显式)都将始终引发错误91。

现在,如果设置了引用 ,则它将 still 用作隐式默认成员调用,以解决分配操作的LHS;如果对象没有默认成员,则运行时错误将为438“对象不支持属性或方法”。

默认成员?

许多类都有默认成员。即,可以省略的构件。一个示例是Collection.Item-这两个语句做的完全相同:

Debug.Print myCollection.Item(i)
Debug.Print myCollection(i) '<~ call to .Item default member is implicit

Rubberduck进行了几项代码检查,这些代码检查可以发现并阻止此错误...以及其他等待被捕获的错误: