So I'm working on a function that works like a normal VLOOKUP but will cycle through and return multiple values. My problem now is that I'm not sure how I can make the following code output multiple values WITH separators. (i.e. "Result1, Result2, Result3") If there's an easier way/work around to what I have here I'll take the advice.
Function KVLOOKUP(LookedValue As Variant, Matrix As Variant, Column As Integer) As Variant
Dim Result() As Integer
Dim i As Integer
Dim Counter As Long
If IsObject(Matrix) Then Matrix = Matrix.Value
On Error Resume Next
Do
i = i + 1
Counter = UBound(Matrix, i)
Loop Until Err.Number <> 0
If Counter < Column Then KVLOOKUP = CVErr(xlErrNum): Exit Function
Counter = 0
For i = LBound(Matrix, 1) To UBound(Matrix, 1)
If Matrix(i, 1) = LookedValue Then
Counter = Counter + 1
ReDim Preserve Result(1 To Counter)
Result(Counter) = Matrix(i, Column)
End If
Next i
On Error GoTo 0
If Counter = 0 Then
KVLOOKUP = CVErr(xlErrNA)
Else
KVLOOKUP = Insert command to add separator and output values here.
End If
End Function