
在VBA中
可以使用Application.Match函数来查找元素在数组中的索引
代码如下:
Sub FindIndex()
' 定义变量
Dim arr() As Variant
Dim searchValue As Variant
Dim index As Variant
' 定义数组-手动生成一个一维数组
arr = Array("Apple", "Banana", "Orange", "Mango", "Grapes")
' 设置要查找的值
searchValue = "Orange"
' 使用Match函数查找索引
index = Application.Match(searchValue, arr, 0)
' 判断是否找到索引
If IsError(index) Then
MsgBox "元素未找到!"
Else
MsgBox "元素 " & searchValue & " 的索引为: " & index
End If
End Sub
可以根据需要修改数组和搜索值来套用代码。
这段代码将返回元素在数组中的索引,
如果未找到则显示一个错误消息框。