如何在 VBA 中应用 MATCH() 函数?
How to apply MATCH() function in VBA?
我正在尝试 运行 下面的代码但是得到
Error 1004: Unable to get the Match property of the WorksheetFunction class.
我理解如果没有匹配,MATCH() 函数 returns #N/A,所以没有必要将它分配给 INDEX变量(而且,我认为它也可能会导致错误)。
我如何解释这种可能性?
Sub Debugging()
Workbooks("Problem.xls").Worksheets(1).Activate
Cash_Rows = 5
Share_Rows = 6
If Cash_Rows <= Share_Rows Then
Range("A1:A" & Cash_Rows).Select
With Selection.Interior
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399975585192419
End With
Count_Cash = Application.WorksheetFunction.CountIf(Range("A:A"), "L*")
For Each cell In Range("A1:A" & Cash_Rows)
If CStr(cell.Value) Like "L*" Then
Range("A" & cell.Row & ":" & "D" & cell.Row).Interior.Color = 65535
Dim Index As Integer
Index = Application.WorksheetFunction.Match(CStr(cell.Value), Range("F2:" & "F" & Share_Rows), 0)
Range("F" & Index & ":" & "I" & Index).Interior.Color = 65535
End If
Next
If Count_Cash = 0 Then
MsgBox "You do not have any matching ID+Amount between Cash and Shares booking. It's OK!"
Else
MsgBox "You have " & Count_Cash & " matching transactions. Check them!"
End If
Else
MsgBox "Do not worry. Be happy!"
End If
End Sub
使用 Application.Match
而不是 Application.WorksheetFunction.Match
。该错误表明 Match
本身丢失了,而不是 Match
的参数有问题。 (虽然不知道为什么 Match
应该丢失!)
正如您在评论中提到的,Dim Index as Variant
而不是 Integer
。 (顺便说一下,use Long
instead of Integer
除非你调用一个 16 位的 API 函数。)
Per this answer, Application.Match
returns 如果匹配失败 (#N/A
) 则错误变体。要对此进行测试,请使用 IsError
:
If Not IsError(Index) Then
Dim idxstr as String: idxstr = CStr(Index)
' ^^ Make sure you don't get surprised by how the Variant converts
Range("F" & idxstr & ":" & "I" & idxstr).Interior.Color = 65535
End If
我正在尝试 运行 下面的代码但是得到
Error 1004: Unable to get the Match property of the WorksheetFunction class.
我理解如果没有匹配,MATCH() 函数 returns #N/A,所以没有必要将它分配给 INDEX变量(而且,我认为它也可能会导致错误)。
我如何解释这种可能性?
Sub Debugging()
Workbooks("Problem.xls").Worksheets(1).Activate
Cash_Rows = 5
Share_Rows = 6
If Cash_Rows <= Share_Rows Then
Range("A1:A" & Cash_Rows).Select
With Selection.Interior
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399975585192419
End With
Count_Cash = Application.WorksheetFunction.CountIf(Range("A:A"), "L*")
For Each cell In Range("A1:A" & Cash_Rows)
If CStr(cell.Value) Like "L*" Then
Range("A" & cell.Row & ":" & "D" & cell.Row).Interior.Color = 65535
Dim Index As Integer
Index = Application.WorksheetFunction.Match(CStr(cell.Value), Range("F2:" & "F" & Share_Rows), 0)
Range("F" & Index & ":" & "I" & Index).Interior.Color = 65535
End If
Next
If Count_Cash = 0 Then
MsgBox "You do not have any matching ID+Amount between Cash and Shares booking. It's OK!"
Else
MsgBox "You have " & Count_Cash & " matching transactions. Check them!"
End If
Else
MsgBox "Do not worry. Be happy!"
End If
End Sub
使用
Application.Match
而不是Application.WorksheetFunction.Match
。该错误表明Match
本身丢失了,而不是Match
的参数有问题。 (虽然不知道为什么Match
应该丢失!)正如您在评论中提到的,
Dim Index as Variant
而不是Integer
。 (顺便说一下,useLong
instead ofInteger
除非你调用一个 16 位的 API 函数。)Per this answer,
Application.Match
returns 如果匹配失败 (#N/A
) 则错误变体。要对此进行测试,请使用IsError
:If Not IsError(Index) Then Dim idxstr as String: idxstr = CStr(Index) ' ^^ Make sure you don't get surprised by how the Variant converts Range("F" & idxstr & ":" & "I" & idxstr).Interior.Color = 65535 End If