在二维数组中查找位置

时间:2014-03-18 11:38:20

标签: vba

我有一个二维数组:

(1, 1) = X  (1, 2) = [Empty]  (1, 3) = [Empty]
(2, 1) = Y  (2, 2) = [Empty]  (2, 3) = [Empty]
(3, 1) = Z  (3, 2) = [Empty]  (3, 3) = [Empty]

我想在第2和第3列存储数据,其中行号是通过匹配第一列中的值与提供的某个特定值来确定的。有没有办法找到存在Z的数组的行号,而不必遍历整个列?我正在寻找在一维数组上使用WorksheetFunction.Match的等价物。

为了解决我的问题,我可以创建两个数组,其中第一个将具有一个维度,并将存储值以查找,第二个将存储其余列。不过,我宁愿只有一个。

3 个答案:

答案 0 :(得分:1)

您可以使用Index()来处理数组中的区域,然后允许您使用匹配。但是,我总是发现Excel函数在VBA数组上使用时速度极慢,特别是在较大的数组上。

我猜测并且说实际上循环通过将是你最好的选择。或者,根据您的使用情况,使用不同的存储机制,具有键/值查找的东西,如集合或Scripting.Dictionary可能会为您提供最佳性能

修改

为了记录,我再次声明我不会这样做,它在大型阵列上很慢,但你可以这样做:

Sub test()
    Dim arr(1 To 3, 1 To 3)

    arr(1, 1) = "X"
    arr(2, 1) = "Y"
    arr(3, 1) = "Z"

    With Application
        MsgBox .Match("Z", .Index(arr, 0, 1), 0)
    End With

End Sub

答案 1 :(得分:1)

尝试此功能

Public Function posInArray(ByVal itemSearched As Variant,ByVal aArray As Variant) As Long  

Dim pos As Long, item As Variant  

posInArray = 0  
If IsArray(aArray) Then  
If Not isEmpty(aArray) Then  
    pos = 1  
    For Each item In aArray  
        If itemSearched = item Then  
            posInArray = pos  
            Exit Function  
        End If
        pos = pos + 1  
    Next item  
    posInArray = 0
End If  
End If  

End Function

答案 2 :(得分:0)

'To determine if a multi-dimension array is allocated (or empty)
'Works for any-dimension arrays, even one-dimension arrays
Public Function isArrayAllocated(ByVal aArray As Variant) As Boolean

On Error Resume Next
isArrayAllocated = IsArray(aArray) And Not IsError(LBound(aArray, 1)) And LBound(aArray, 1) <= UBound(aArray, 1)
Err.Clear: On Error GoTo 0

End Function

'To determine the number of dimensions of an array
'Returns -1 if there is an error
Public Function nbrDimensions(ByVal aArray As Variant) As Long
Dim x As Long, tmpVal As Long

If Not IsArray(aArray) Then
    nbrDimensions = -1
    Exit Function
End If

On Error GoTo finalDimension
For x = 1 To 65536 'Maximum number of dimensions (size limit) for an array that will work with worksheets under Excel VBA
    tmpVal = LBound(aArray, x)
Next x

finalDimension:
nbrDimensions = x - 1
Err.Clear: On Error GoTo 0

End Function

'*****************************************************************************************************************************
'To return an array containing al the coordinates from a specified two-dimension array that have the searched item as value
'Returns an empty array if there is an error or no data
'Returns coordinates in the form of x,y
'*****************************************************************************************************************************
Public Function makeArrayFoundXYIn2DimArray(ByVal itemSearched As Variant, ByVal aArray As Variant) As Variant
Dim tmpArr As Variant, x As Long, y As Long, z As Long

tmpArr = Array()
If IsArray(aArray) Then
    If isArrayAllocated(aArray) And nbrDimensions(aArray) = 2 Then
        z = 0
        For x = LBound(aArray, 1) To UBound(aArray, 1)
            For y = LBound(aArray, 2) To UBound(aArray, 2)
                If itemSearched = aArray(x, y) Then
                    If z = 0 Then
                        ReDim tmpArr(0 To 0)
                    Else
                        ReDim Preserve tmpArr(0 To UBound(tmpArr) + 1)
                    End If
                    tmpArr(z) = CStr(x) + "," + CStr(y)
                    z = z + 1
                End If
            Next y
        Next x
    End If
End If
makeArrayFoundXYIn2DimArray = tmpArr
Erase tmpArr

End Function
shareeditflag