I have one worksheet Relationships
which contains data entries specifying a relationship between A and B and some related data.
In another worksheet, I have rows for each A and would like a cell that effectively queries the Relationships
data and summarizes all B who have a relationship with A (and some of the related data).
I have written a VBA lookup to do this, but I imagine it is the least efficient approach possible, as it loops through the entire range in Relationships
, determines if the subject row is one relating to A, and if it is, it starts concatenating a string which it ultimately returns.
Just wanted to know if anyone can point me in the right direction for an approach to this problem that is hopefully more efficient? VLOOKUP
doesn't seem to be very helpful because of the "multiple match" issue.
As requested, an example of what I used - it works fine, but I fear it is slow and will become crippling as the data grows.
Function oLookup(lookupval, lookuprange As Range, entity As Long, addlbylines As Long, name As Long, title As Long, Optional count As Variant)
Dim x As Range
Dim result As String
result = ""
For Each x In lookuprange.Rows
If (x.Cells(1, entity).Value = lookupval And lookupval <> "") Then
result = result & x.Cells(1, name) & " (" & x.Cells(1, title) & ")"
If Not IsMissing(count) Then result = result & " [Freq: " & x.Cells(1, count) & "]"
result = result & Chr(10)
End If
Next x
If result = "" Then result = "No data" Else result = Left(result, Len(result) - 1)
oLookup = result
End Function