VBA 匹配函数查找不存在的值
VBA Match Function Finding Value That Doesn't Exist
我有一些 VBA 代码可以执行与不同工作表的匹配。然后使用从此匹配返回的行来确定 If
语句的结果。这是我的代码:
On Error Resume Next 'Accuracy Index Match Start
aMatchRow = Application.Match(summarySheet.Cells(accuracyRow, 3), aCommSheet.Range("C:C"), 0)
On Error GoTo 0
With summarySheet.Cells(accuracyRow, 15)
If aMatchRow > 0 Then
.Value = aCommSheet.Cells(aMatchRow, 15)
Else
.Value = "COMMENT REQUIRED"
End If
End With
我遇到的问题是,即使没有匹配项,也会返回一个值。因此,例如,在 summarySheet.Cells(accuracyRow, 3)
在 aCommSheet.Range("C:C")
中没有匹配项的情况下,我仍然会返回一个行值,然后将其馈送到 If
语句中,因此错误的值返回到 summarySheet.Cells(accuracyRow, 15)
.
如果不匹配,则应执行"ELSE"。但是无论如何"If"都在执行
扩展@GSerg 的评论:
Dim m
m = Application.Match(summarySheet.Cells(accuracyRow, 3), aCommSheet.Range("C:C"), 0)
With summarySheet.Cells(accuracyRow, 15)
If Not IsError(m) Then '<<< test for no match here
.Value = aCommSheet.Cells(m, 15)
Else
.Value = "COMMENT REQUIRED"
End If
End With
我有一些 VBA 代码可以执行与不同工作表的匹配。然后使用从此匹配返回的行来确定 If
语句的结果。这是我的代码:
On Error Resume Next 'Accuracy Index Match Start
aMatchRow = Application.Match(summarySheet.Cells(accuracyRow, 3), aCommSheet.Range("C:C"), 0)
On Error GoTo 0
With summarySheet.Cells(accuracyRow, 15)
If aMatchRow > 0 Then
.Value = aCommSheet.Cells(aMatchRow, 15)
Else
.Value = "COMMENT REQUIRED"
End If
End With
我遇到的问题是,即使没有匹配项,也会返回一个值。因此,例如,在 summarySheet.Cells(accuracyRow, 3)
在 aCommSheet.Range("C:C")
中没有匹配项的情况下,我仍然会返回一个行值,然后将其馈送到 If
语句中,因此错误的值返回到 summarySheet.Cells(accuracyRow, 15)
.
如果不匹配,则应执行"ELSE"。但是无论如何"If"都在执行
扩展@GSerg 的评论:
Dim m
m = Application.Match(summarySheet.Cells(accuracyRow, 3), aCommSheet.Range("C:C"), 0)
With summarySheet.Cells(accuracyRow, 15)
If Not IsError(m) Then '<<< test for no match here
.Value = aCommSheet.Cells(m, 15)
Else
.Value = "COMMENT REQUIRED"
End If
End With