VBA编译错误:需要对象(此时获取灰色) 我自己做了一些简单的程序设计,只是C++和简单的VBA。我不太熟悉VBA语法,我正在尝试
我正在研究一个函数来比较两组单元格。如果单元格相同,则返回VBA编译错误:需要对象(此时获取灰色) 我自己做了一些简单的程序设计,只是C++和简单的VBA。我不太熟悉VBA语法,我正在尝试,vba,excel,runtime-error,Vba,Excel,Runtime Error,我正在研究一个函数来比较两组单元格。如果单元格相同,则返回False,如果单元格不相同则返回True。问题是我无法让if语句工作,以创建我的计数,最终将得到我的True或False 我遇到问题并收到运行时错误424:需要对象 If firstCaseVal Is secondCaseVal Then k = k + 1 End If 以下是完整的功能: Function histMisMatch() As Boolean Dim matchCountRows As Integer Di
False
,如果单元格不相同则返回True
。问题是我无法让if语句工作,以创建我的计数,最终将得到我的True
或False
我遇到问题并收到运行时错误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()如果找不到匹配项,则返回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
我以前尝试过此操作,但遇到以下错误:运行时错误“91”:对象变量或块变量未设置。向matchCountRows添加“.”将导致编译错误。删除了“.”,现在带有“=”的if语句给出了运行时错误91:object变量或with block变量not setSee my edit。我认为资产的使用是个问题。我也改变了这段时间的安排。您希望使用ws2的列,因此句点在列之前。Asset是一个用户表单标签,它只是从另一个userformNumeric传递给它的值,不设置字符串等数值。您可以编写SecondCaseVal=secondcase。未设置secondcaseVal=secondcase。我建议您将值设置为整数/长/双精度,以避免混淆(但不是必需的)。我认为程序将其视为一个范围对象,因为您将其设置为与您在其中找到信息的单元格相等。不,它们并不总是匹配。。。检查第1列到第9列中所需行中的单元格值是否已经存在,如果已经存在,则不会复制和忽略它们,但是,如果它们没有出现在
ws3
中,那么它们会被复制到ws3
中,这并不是因为它解决了我得到的运行时错误,而是因为它是解决我的问题的更简单、更优雅的解决方案。非常感谢你