First and foremost I need to establish as to whether or not the below UDF combined with a Lookup is compatible with Excel 2010
UDF is from here, Oscar Cronquist – "Get Digital Help"
My Lookup formula in R12 is : ={vbaVLOOKUP(P12,E6:F15,2)}
UDF is this:-
While the UDF works with the 1st Vlookup formula in R12 to give the 1st instance of the look up value and gives the expected value.
However the 2nd Vlookup formula in R13 that is supposed to give the 2nd instance of lookup value is giving me a repeat of the first, it’s the same for R14,R15………
UDF is from here, Oscar Cronquist – "Get Digital Help"
My Lookup formula in R12 is : ={vbaVLOOKUP(P12,E6:F15,2)}
UDF is this:-
VBA Code:
'
'UDF along with a Vlookup formula to find & list multiple matches of selected value
'
Function vbaVlookup(lookup_value As Range, tbl As Range, col_index_num As Integer, Optional layout As String = "v")
'Declare variables and data types
Dim r As Single, Lrow, Lcol As Single, temp() As Variant
'Redimension array variable temp
ReDim temp(0)
'Iterate through cells in cell range
For r = 1 To tbl.Rows.Count
'Check if lookup_value is equal to cell value
If lookup_value = tbl.Cells(r, 1) Then
'Save cell value to array variable temp
temp(UBound(temp)) = tbl.Cells(r, col_index_num)
'Add anoher container to array variable temp
ReDim Preserve temp(UBound(temp) + 1)
End If
Next r
If layout = "h" Then
'Save the number of columns the user has entered this User Defined Function in.
Lcol = Range(Application.Caller.Address).Columns.Count
'Iterate through each container in array variable temp that won't be populated
For r = UBound(temp) To Lcol
'Save a blank to array container
temp(UBound(temp)) = ""
'Increase the size of array variable temp with 1
ReDim Preserve temp(UBound(temp) + 1)
Next r
'Decrease the size of array variable temp with 1
ReDim Preserve temp(UBound(temp) - 1)
'Return values to worksheet
vbaVlookup = temp
'These lines will be rund if variable layout is not equal to h
Else
'Save the number of rows the user has entered this User Defined Function in
Lrow = Range(Application.Caller.Address).Rows.Count
'Iterate through empty cells and save nothing to them in order to avoid an error being displayed
For r = UBound(temp) To Lrow
temp(UBound(temp)) = ""
ReDim Preserve temp(UBound(temp) + 1)
Next r
'Decrease the size of array variable temp with 1
ReDim Preserve temp(UBound(temp) - 1)
'Return temp variable to worksheet with values rearranged vertically
vbaVlookup = Application.Transpose(temp)
End If
End Function
While the UDF works with the 1st Vlookup formula in R12 to give the 1st instance of the look up value and gives the expected value.
However the 2nd Vlookup formula in R13 that is supposed to give the 2nd instance of lookup value is giving me a repeat of the first, it’s the same for R14,R15………