gpt4 book ai didi

excel - 在 VBA 中使用 application.match 时出现错误 2042

转载 作者:行者123 更新时间:2023-12-04 21:38:11 25 4
gpt4 key购买 nike

我试图在 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 , 否则索引。
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

关于excel - 在 VBA 中使用 application.match 时出现错误 2042,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27979154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com