Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据其索引查找数组值(Excel VBA)_Excel_Vba - Fatal编程技术网

根据其索引查找数组值(Excel VBA)

根据其索引查找数组值(Excel VBA),excel,vba,Excel,Vba,我有一个使用Application.Match在数组中查找位置的代码。但我如何反过来做:根据位置在数组中查找值 它是一个一维数组“posArr()作为变量”。因此,如果数组包含“牛奶、可乐、水”,我想找到位置3 ill中的值“Ar(3)=posArr”?-戈达顿8分钟前 关于我的例子,请看这两个例子 找出位置3的内容 Sub Sample() Dim posArr Dim s As String Dim pos As Long pos = 3 '~~> 3

我有一个使用Application.Match在数组中查找位置的代码。但我如何反过来做:根据位置在数组中查找值

它是一个一维数组“posArr()作为变量”。因此,如果数组包含“牛奶、可乐、水”,我想找到位置3 ill中的值“Ar(3)=posArr”?-戈达顿8分钟前

关于我的例子,请看这两个例子

找出位置3的内容

Sub Sample()
    Dim posArr
    Dim s As String
    Dim pos As Long

    pos = 3 '~~> 3rd Position

    s = "milk,coke,water"
    posArr = Split(s, ",")

    Debug.Print posArr(pos - 1)
End Sub
检查位置3处的值是否为“水”


之所以我们使用
-1
,是因为最低界限是
0

什么数组?如何申报?它是基于索引的数组吗?需要更多信息
如果Ar(i)=Value
i
是要搜索的位置和
Value
时。假设:它是1D数组它是一个1D数组“posArr()作为变量”。因此,如果数组包含“牛奶、可乐、水”,并且我想在位置3 ill“Ar(3)=posArr”中查找值,请稍后发布答案。因此,在我的代码中,我有一个数组,它使用Application.Match(arrItem)获取位置。使用您的代码执行以下操作:调试。打印posArr(pos arrItem)?
Debug.Print posArr(arrItem-1)
Sub Sample()
    Dim posArr
    Dim s As String
    Dim pos As Long

    pos = 3 '~~> 3rd Position

    s = "milk,coke,water"
    posArr = Split(s, ",")

    If posArr(pos - 1) = "water" Then MsgBox "What an awesome guess!"
End Sub