Return Index of an Element in an Array Excel VBA

后端 未结 7 1612
悲&欢浪女
悲&欢浪女 2020-11-29 20:42

I have an array prLst that is a list of integers. The integers are not sorted, because their position in the array represents a particular column on a spreadsheet. I want t

相关标签:
7条回答
  • 2020-11-29 21:12
    'To return the position of an element within any-dimension array  
    'Returns 0 if the element is not in the array, and -1 if there is an error  
    Public Function posInArray(ByVal itemSearched As Variant, ByVal aArray As Variant) As Long  
    Dim pos As Long, item As Variant  
    
    posInArray = -1  
    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
    
    0 讨论(0)
提交回复
热议问题