簡體   English   中英

Excel VBA:無法獲取匹配項,錯誤“無法獲取 WorksheetFunction 類的匹配屬性”

[英]Excel VBA: Can't get a match, error "Unable to get the Match property of the WorksheetFunction class"

出於對一切美好事物的熱愛,我似乎無法讓它發揮作用。 我不斷收到上面提到的錯誤。

我有這個表,我試圖找出代碼是否與另一列中某個地方的自己的子代碼匹配,但是它出錯了。 非常感謝您的幫助。

在此處輸入圖片說明

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變量,您可以在內部分配這個變量True的部分If/Then語句中,如果你需要找出其中找到匹配。

作為另一種選擇,這也可以通過將下面的公式放在單元格 C2 中並將其向下拖動到 C23 來完成。

=IF(COUNTIF($A$2:$A$23,B2)>=1,"YES","NO")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM