3 回答
TA贡献1785条经验 获得超8个赞
如果我们要谈论性能,那么没有替代品可以运行一些测试。以我的经验,Application.Match()比调用使用循环的函数要慢十倍。
Sub Tester()
Dim i As Long, b, t
Dim arr(1 To 100) As String
For i = 1 To 100
arr(i) = "Value_" & i
Next i
t = Timer
For i = 1 To 100000
b = Contains(arr, "Value_50")
Next i
Debug.Print "Contains", Timer - t
t = Timer
For i = 1 To 100000
b = Application.Match(arr, "Value_50", False)
Next i
Debug.Print "Match", Timer - t
End Sub
Function Contains(arr, v) As Boolean
Dim rv As Boolean, lb As Long, ub As Long, i As Long
lb = LBound(arr)
ub = UBound(arr)
For i = lb To ub
If arr(i) = v Then
rv = True
Exit For
End If
Next i
Contains = rv
End Function
输出:
Contains 0.8710938
Match 4.210938
- 3 回答
- 0 关注
- 549 浏览
添加回答
举报