[英]Error 2042 when using application.match in VBA
我試圖在 VBA 中找到數組中整數的索引。
我這樣創建了數組。
Dim spot(11) as Integer
For index = 1 To NoOfSpotValues
spot(index) = Cells(15 + index, 7)
Next
當我做:
posOfSpot = Application.Match(0, spot, False)
它給了我一個錯誤,如posOfSpot = Error 2042
。
我該如何解決? 嘗試搜索。 需要一些指導。
編輯:
Function Find(ByVal Value As Variant, arr As Variant) As Integer
If arr.Exists(Value) Then
index = arr(Value)
Else
index = -1
End If
如果您願意使用公式而不是 VBA,您可以這樣做
=MATCH(0 ,G16:G26,0)
如果找不到該值,它將返回#N/A
,否則返回 index 。
Sub Find(ByVal Value As Integer)
Dim spotData
Dim index As Integer
Set spotData = CreateObject("Scripting.Dictionary")
For index = 1 To 11
spotData.Add Cells(15 + index, 7).Value, index
Next
If spotData.Exists(Value) Then
index = spotData(Value)
Else
index = -1
End If
Set spotData = Nothing
Debug.Print index
End Sub
Dim spot(1 to 11) as Long
Dim posOfSpot& , index&
For index = 1 To NoOfSpotValues
spot(index) = Cells(15 + index, 7).value
Next index
posOfSpot = Application.Match(0, spot, 0)
msgbox posOfSpot
試過這個,有效。
但是,在數組中使用一個簡單的循環來查找您的值,代碼會更快
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.