gpt4 book ai didi

vba - Excel VBA : Can't get a match, 错误 "Unable to get the Match property of the WorksheetFunction class"

转载 作者:行者123 更新时间:2023-12-01 19:16:55 30 4
gpt4 key购买 nike

出于对一切美好事物的热爱,我似乎无法让它发挥作用。我不断收到上述错误。

我有这个表,我试图找出代码是否与另一列中某处的它自己的子代码匹配,但是它出错了。非常感谢您的帮助。

enter image description here

Sub testing()

Dim m1 As long
Dim myrange As Range

Set myrange = Worksheets("Sheet1").Range("B2:B23")

For e = 2 To 23
m1= Application.WorksheetFunction.Match(Cells(e, 1).Value, myrange, 0)

If m1 > 0 Then
Cells(e, 3).Value = "Yes"
Else
Cells(e, 3).Value = "No"
End If
Next e

MsgBox "Complete!"

End Sub

最佳答案

使用Application.Match函数可以更好地捕获错误。使用 WorksheetFunction.Match 时,如果未找到匹配项,则会返回错误,这正是您所遇到的情况。

If Not IsError(Application.Match(Cells(e, 1).Value, myrange, 0)) Then
'Do stuff when the match is found
Cells(e, 3).Value = "Yes"
Else:
Cells(e, 3).Value = "No"
End If

您还可以使用 CountIf 函数:

If Application.WorksheetFunction.CountIf(myRange, Cells(e,1).Value) > 0 Then
Cells(e,3).Value = "Yes"
Else:
Cells(e,3).Value = "No"
End If

这两种方法都不需要您使用 m1 变量,您可以在 If/ThenTrue 部分内分配此变量语句,如果您需要确定在哪里找到匹配项。

关于vba - Excel VBA : Can't get a match, 错误 "Unable to get the Match property of the WorksheetFunction class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17751443/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com