VBA "index" in a function

Rolly_Sefu

Board Regular
Joined
Oct 25, 2013
Messages
149
Hy

As an index formula would work =Index(A1:A10,Match(C1,B1:B10,0)) this is the most basic index and match.

I need to get multiple results in the same cell divided by a comma based on a value: C1

So I created this function:

Code:
Function speciallookup(result_range As Range, lookup_range As Range, lookup_criteria As Double) As String
'result_range=A1:A10
'lookup_range=B1:B10
'lookup_criteria=C1
For Each cell In lookup_range
    If cell = lookup_criteria Then
        x = x & cell.Offset(0, -1) & ", "
    End If
Next cell
speciallookup = Left(x, Len(x) - 2)
End Function

My question is how can I get the result from result_range and not use the cell.offset ?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try...

Code:
Function speciallookup(result_range As Range, lookup_range As Range, lookup_criteria As Double) As Variant
    Dim x As String
    Dim i As Long
    
    If lookup_range.Rows.Count > 1 And lookup_range.Columns.Count > 1 Or _
        result_range.Rows.Count > 1 And result_range.Columns.Count > 1 Or _
        lookup_range.Cells.Count <> result_range.Cells.Count Then
            speciallookup = CVErr(xlErrNA)
            Exit Function
    End If
    
    x = ""
    For i = 1 To lookup_range.Cells.Count
        If lookup_range(i).Value = lookup_criteria Then
            x = x & ", " & result_range(i).Value
        End If
    Next i
    
    If Len(x) = 0 Then
        speciallookup = CVErr(xlErrNA)
    Else
        x = Mid(x, 3)
        speciallookup = x
    End If
    
End Function

Note that the function will return #N/A when no matching entries are found. It also returns #N/A when the ranges are not the same size.

Hope this helps!
 
Upvote 0
Hy, thank you, this is exactly what I wanted.

Did not occur to me to use the range as item ( lookup_range(i) ) and then just give the same item nr from the other range.

Briliant.

Thanks.
 
Upvote 0
You're very welcome. Glad I could help.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,215,044
Messages
6,122,827
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top