I am writing a Excel UDF function to find the index no of an element of an array. But, I encounter some technical challenges. The named range that I pass into the UDF does not have value. I observe that vNamedrange(row, 1) = empty if I hover on vNamedrange(row,
1). I have been trying to solve the problem for last few days but I have not found a solution. Can someone guide me on this challenge? Thanks in advance.
The UDF is set out below:
Background:
- Findme = C6 = 2
- vNamedrange = vNumlist = B5:B10 =1,2,3,4,5,6
- The UDF return zero.
Function GetVIndex(Findme As Variant, vNamedrange As Variant) As Long
'To get the index of a named range
Dim row As Long
Dim iRows As Long
Dim Pos As Long
On Error GoTo ErrorHandler
vNamedrange = vNamedrange.Value2 'Convert Range to array
iRows = UBound(vNamedrange)
ReDim vNamedrange(1 To iRows, 1 To 1)
'Findme = Findme.Value2
For row = 1 To iRows
If Findme = vNamedrange(row, 1) Then
Pos = row
Exit For
End If
Next
GetVIndex = Pos
Exit Function
ErrorHandler:
GetVIndex = CVErr(xlErrValue)
End Function