Look up all occurences and merge results (consolidate)

Function FindNth(rTable As Range, Val1 As Variant, ResultCol As Integer) As String

'''''''''''''''''''''''''''''''''''''''

'Written by OzGrid Business Applications

'www.ozgrid.com

' Finds ALL occurences in the first Column of a table.
' Merges results
'''''''''''''''''''''''''''''''''''''''
'Used like;
'=FindNth($A$1:$B$37,"ALFA5010",2)

Dim lLoop As Long
Dim rFound As Range
Dim strResults As String

With rTable.Columns(1)
Set rFound = .Cells(, 1)
For lLoop = 1 To WorksheetFunction.CountIf(.Cells, Val1)

Set rFound = .Cells.Find(Val1, rFound, xlValues, xlWhole, xlNext, xlRows, False)
If strResults <> vbNullString Then
strResults = strResults & rFound(1, ResultCol) ' Had "& Chr(10) &" to add CR/LF, but I removed it
Else
strResults = rFound(1, ResultCol)
End If

Next lLoop
End With
FindNth = RTrim(strResults)
End Function

source

Comments are closed.