Created
February 13, 2013 19:59
-
-
Save meganmcchesney/4947657 to your computer and use it in GitHub Desktop.
VBA Macro "VLookupAll", which uses a vlookup of a column to produce a concatenated string of related cells. Runs with the formula:
=vlookupall(A,A:B,2, ", ")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function vlookupall(sSearch As String, rRange As Range, _ | |
Optional lLookupCol As Long = 2, Optional sDel As String = ",") As String | |
'Vlookupall searches in first column of rRange for sSearch and returns | |
'corresponding values of column lLookupCol if sSearch was found. All these | |
'lookup values are being concatenated, delimited by sDel and returned in | |
'one string. If lLookupCol is negative then rRange must not have more than | |
'one column. | |
'Reverse("moc.LiborPlus.www") PB 16-Sep-2010 V0.20 | |
Dim i As Long, sTemp As String | |
If lLookupCol > rRange.Columns.Count Or sSearch = "" Or _ | |
(lLookupCol < 0 And rRange.Columns.Count > 1) Then | |
vlookupall = CVErr(xlErrValue) | |
Exit Function | |
End If | |
vlookupall = "" | |
For i = 1 To rRange.Rows.Count | |
If rRange(i, 1).Text = sSearch Then | |
If lLookupCol >= 0 Then | |
vlookupall = vlookupall & sTemp & rRange(i, lLookupCol).Text | |
Else | |
vlookupall = vlookupall & sTemp & rRange(i).Offset(0, lLookupCol).Text | |
End If | |
sTemp = sDel | |
End If | |
Next i | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment