PDA

View Full Version : Sumif for text + concatenation



Kalts
05-18-2012, 02:48 AM
Hello
I need a code to solve my problem. I want to know how I can get information/values from all the cells that matches with criteria and not to sum the values but to create a concatenated list into one cell. Next I want to separate that lastly greated cell into columns, but in a way where the cell data separation into columns is in real time(it will not leave old data on the column row when values in the cell gets shorter and it refreshes or loops so if new information comes to the cell the columns are corrected accordingly and immediately).

Tinbendr
05-18-2012, 06:18 AM
We need more details. A sample sheet with sample contents and expect results would be helpful.

mikerickson
05-18-2012, 07:10 AM
The UDF ConcatIf (whose arguments mimic those of SumIf) sounds like it might do what you want.
Function ConcatIf(ByVal compareRange As Range, ByVal xCriteria As Variant, Optional ByVal stringsRange As Range, _
Optional Delimiter As String, Optional NoDuplicates As Boolean) As String

Rem the first three argumens of ConcatIf mirror those of SUMIF
Rem the Delimiter and NoDuplicates arguments are optional (default "" and False)
Dim i As Long, j As Long

With compareRange.Parent
Set compareRange = Application.Intersect(compareRange, Range(.UsedRange, .Range("a1")))
End With

If compareRange Is Nothing Then Exit Function
If stringsRange Is Nothing Then Set stringsRange = compareRange
Set stringsRange = compareRange.Offset(stringsRange.Row - compareRange.Row, _
stringsRange.Column - compareRange.Column)

For i = 1 To compareRange.Rows.Count
For j = 1 To compareRange.Columns.Count
If (Application.CountIf(compareRange.Cells(i, j), xCriteria) = 1) Then
If InStr(ConcatIf, Delimiter & CStr(stringsRange.Cells(i, j))) <> 0 Imp Not (NoDuplicates) Then
ConcatIf = ConcatIf & Delimiter & CStr(stringsRange.Cells(i, j))
End If
End If
Next j
Next i
ConcatIf = Mid(ConcatIf, Len(Delimiter) + 1)
End Function