VBA Compile Error: Object Required (getting greys at this point)

VBA Compile Error: Object Required (getting greys at this point)

我自己的一些背景:我一生中做过一些简单的编程,但只是 C++ 和简单的 VBA。我不太熟悉 VBA 语法,我正在尝试。

我正在开发一个函数来比较 2 组单元格。如果单元格相同,则返回 False,如果不相同则返回 True。问题是我无法让我的 if 语句工作以创建我的计数,这最终会给我 TrueFalse

我遇到问题并收到 运行 时间错误 424:需要对象的行。

If firstCaseVal Is secondCaseVal Then
    k = k + 1
End If

完整函数如下:

Function histMisMatch() As Boolean
Dim matchCountRows As Integer
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim k As Integer
Dim firstCaseVal As Variant, secondCaseVal As Variant, firstCase As Variant, secondCase As Variant        
    With ws2
        matchCountRows = Columns(1).Find(Asset, lookat:=xlWhole).Row
    End With

    Set ws2 = Sheets("Sheet2")
    Set ws3 = Sheets("Sheet3")

    k = 0

    For i = 1 To 9
        firstCase = ws2.Cells(matchCountRows, i).Value
        firstCaseVal = firstCase

        With ws3
        Set secondCase = ws3.Columns(i).Find(firstCase, lookat:=xlWhole)
        Set secondCaseVal = secondCase            
        End With

        If firstCaseVal Is secondCaseVal Then
            k = k + 1
        End If            
    Next        

    If k = 9 Then
        histMisMatch = False
    Else
        histMisMatch = True
    End If        
End Function

已更改但仍收到 运行-if 语句中的时间错误 91 的代码

Function histMisMatch() As Boolean
Dim i As Integer
Dim matchCountRows As Integer
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim k As Integer
Dim firstCaseVal As Variant, secondCaseVal As Variant, firstCase As Variant, secondCase As Variant

    Set ws2 = Sheets("Sheet2")
    Set ws3 = Sheets("Sheet3")

    With ws2
        matchCountRows = .Columns(1).Find(Asset, lookat:=xlWhole).Row
    End With 

    k = 0

    For i = 1 To 9
        firstCase = ws2.Cells(matchCountRows, i).Value
        firstCaseVal = firstCase

        With ws3
        Set secondCase = ws3.Columns(i).Find(firstCase, lookat:=xlWhole)
        Set secondCaseVal = secondCase
        End With

        If firstCaseVal = secondCaseVal Then
            k = k + 1
        End If
    Next


    If k = 9 Then
        histMisMatch = False
    Else
        histMisMatch = True
    End If        
End Function

如果您想比较它们,请使用 = 而不是 Is

If firstCaseVal = secondCaseVal Then
    k = k + 1
End If

您的代码有几个问题:

Dim i as integer 'Dim all your variables

Set ws2 = Sheets("Sheet2") 'Set the sheet before using it

With ws2 'You need to add a . before what you write in a With that is to be preceded with the With argument
    matchCountRows = .Columns(1).Find(Asset, lookat:=xlWhole).Row
'What is Asset? If it is a string, you need to write it like "Asset"
End With

If firstCaseVal = secondCaseVal Then 'Use = to compare, not Is
     k = k + 1
End If

我相信如果您进行这些更改,它会奏效。

您似乎在使用 find() 来定位具有特定值的单元格,然后将该单元格中的值与搜索到的值进行比较,当然只要有打。

相反,您可以只测试

If Not secondCase Is Nothing Then 

因为 Find() returns Nothing 如果没有找到匹配项(这就是导致错误的原因 "object variable or with block variable not set")。

编辑 - 一些建议的更改:

Function histMisMatch() As Boolean

    Dim i As Integer
    Dim matchCountRows As Long
    Dim ws2 As Worksheet
    Dim ws3 As Worksheet
    Dim firstCase As Variant

    Set ws2 = Sheets("Sheet2")
    Set ws3 = Sheets("Sheet3")

    matchCountRows = ws2.Columns(1).Find(Asset, lookat:=xlWhole).Row

    For i = 1 To 9
        firstCase = ws2.Cells(matchCountRows, i).Value
        If ws3.Columns(i).Find(firstCase, lookat:=xlWhole) Is Nothing Then
            histMisMatch = True
            Exit Function 'you can stop looking here, since one value was not found...
        End If
    Next
    'got here if no missing values...
    histMisMatch = False

End Function