Concatenate values in different rows if criteria cells match?

iddy

New Member
Joined
Mar 10, 2006
Messages
18
Hi

I'm hoping somebody can help me with a formula for this but all suggestions appreciated. (using Excel 2010 and Windows 7)

I have very large spreadsheets containing Vehicle Application data, the first line of which contains headings. All the other lines contain details of the spare part, the car it is fitted to, and any fitting restrictions.

Column......Column Name
A...........PART_ID
B...........PART_NAME
C...........TERMS_OF_USE
D...........VEHICLE

for rows when columns A,B,and C are the same I'd like to concatenate all the vehicles into a cell in Column E (separated by a colon)

This is an example of what I am looking to end up with (formula to go in column E to get that result)

PART_ID PART_NAME TERMS_OF_USE VEHICLE ALL VEHICLES
1152702 coil spring Lower to 35mm Ford Focus Ford Focus:Honda Civic:VW Golf
1152702 coil spring Lower to 35mm Honda Civic
1152702 coil spring Lower to 35mm VW Golf
1152702 coil spring Lower to 25mm Ford Focus II Ford Focus II:Honda Civic V:VW Golf IV
1152702 coil spring Lower to 25mm Honda Civic V
1152702 coil spring Lower to 25mm VW Golf IV
1152717 coil spring Toyota Avensis Toyota Avensis
1152728 Gas Spring, bonnet Ford Fiesta Ford Fiesta

Thanks for your time
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi not sure if what you needed:

Given in A1:

Excel 2013
ABCDE
1PART_IDPART_NAMETERMS_OF_USEVEHICLECONCAT
21152702JAWW,J,D
31152702JAJW,J,D
41152702JADW,J,D
51152702LADD,G
61152702KAFF
71152702LAGD,G
81152717JBHH
91152728JCQQ
iddy
Cell Formulas
RangeFormula
E2=ConcatIfs($D$2:$D$9,",",$A$2:$A$9,"="&A2,$B$2:$B$9,"="&B2,$C$2:$C$9,"="&C2)


code is:
Code:
Function ConcatIfs(ByVal stringsRange As Range, Delimiter As String, _                            ByVal compareRange1 As Range, ByVal Criteria1 As Variant, _
                            Optional ByVal compareRange2 As Range, Optional ByVal Criteria2 As Variant, _
                            Optional ByVal compareRange3 As Range, Optional ByVal Criteria3 As Variant, _
                            Optional ByVal compareRange4 As Range, Optional ByVal Criteria4 As Variant, _
                            Optional ByVal compareRange5 As Range, Optional ByVal Criteria5 As Variant, _
                            Optional NoDuplicates As Boolean) As String
    'created by mikerickson
    Dim i As Long, j As Long
    With compareRange1.Parent
       Set compareRange1 = Application.Intersect(compareRange1, Range(.UsedRange, .Range("a1")))
    End With
    If compareRange1 Is Nothing Then Exit Function
    If compareRange2 Is Nothing Then
        Set compareRange2 = compareRange1
        Criteria2 = Criteria1
    End If
    If compareRange3 Is Nothing Then
        Set compareRange3 = compareRange1
        Criteria3 = Criteria1
    End If
    If compareRange4 Is Nothing Then
        Set compareRange4 = compareRange1
        Criteria4 = Criteria1
    End If
    If compareRange5 Is Nothing Then
        Set compareRange5 = compareRange1
        Criteria5 = Criteria1
    End If
    Set stringsRange = compareRange1.Offset(stringsRange.Row - compareRange1.Row, _
                                                stringsRange.Column - compareRange1.Column)
    Set compareRange2 = compareRange1.Offset(compareRange2.Row - compareRange1.Row, _
                                                compareRange2.Column - compareRange1.Column)
    
        For i = 1 To compareRange1.Rows.Count
            For j = 1 To compareRange1.Columns.Count
                If (Application.CountIf(compareRange1.Cells(i, j), Criteria1) = 1) _
                    And (Application.CountIf(compareRange2.Cells(i, j), Criteria2) = 1) _
                    And (Application.CountIf(compareRange3.Cells(i, j), Criteria3) = 1) _
                    And (Application.CountIf(compareRange4.Cells(i, j), Criteria4) = 1) _
                    And (Application.CountIf(compareRange5.Cells(i, j), Criteria5) = 1) Then
                    If InStr(ConcatIfs, Delimiter & CStr(stringsRange.Cells(i, j))) <> 0 Imp Not (NoDuplicates) Then
                        ConcatIfs = ConcatIfs & Delimiter & CStr(stringsRange.Cells(i, j))
                    End If
                End If
            Next j
        Next i
        ConcatIfs = Mid(ConcatIfs, Len(Delimiter) + 1)
End Function
 
Upvote 0
Thanks very much for that. It works even when the data is not sorted correctly which is a great added bonus!
Just one more question on the ranges in the formula. My worksheets are typically over 600k rows, with the value in Part_Id typically repeating for 10k rows - so I might have only 10k rows that I need to check together at one time. Am I correct in assuming I can change the absolute values in the formula to the beginning and end of the range and not break the formula? e.g. if my range starts on row 100 and ends on row 200 I can change $D$2:$D$9 to $D$100:$D$200

Thanks again
 
Upvote 0
Most welcome, glad it worked for you.
Yes you may change the range to fit your model, just try to avoid selecting the whole column (D:D) as this may slow down the system.
 
Upvote 0
Hi again,

I tried to put this into a Personal macro workbook to allow me to use it for my real workbooks and I'm having trouble getting it to save.
Not sure whats changed since I tried this when you first posted it but I am getting a compile error:invalid character now (I am copying the code from the forum into Visual Basic on the Developer tab.

All of the following code is showing in red with the first underscore highlighted (Function ConcatIfs(ByVal stringsRange As Range, Delimiter As String, _)

Code:
Function ConcatIfs(ByVal stringsRange As Range, Delimiter As String, _                            ByVal compareRange1 As Range, ByVal Criteria1 As Variant, _
                            Optional ByVal compareRange2 As Range, Optional ByVal Criteria2 As Variant, _
                            Optional ByVal compareRange3 As Range, Optional ByVal Criteria3 As Variant, _
                            Optional ByVal compareRange4 As Range, Optional ByVal Criteria4 As Variant, _
                            Optional ByVal compareRange5 As Range, Optional ByVal Criteria5 As Variant, _
                            Optional NoDuplicates As Boolean) As String

Thanks in advance for the help.
 
Upvote 0
maybe:
Code:
Function ConcatIfs(ByVal stringsRange As Range, Delimiter As String, _                            
                            ByVal compareRange1 As Range, ByVal Criteria1 As Variant, _
                            Optional ByVal compareRange2 As Range, Optional ByVal Criteria2 As Variant, _
                            Optional ByVal compareRange3 As Range, Optional ByVal Criteria3 As Variant, _
                            Optional ByVal compareRange4 As Range, Optional ByVal Criteria4 As Variant, _
                            Optional ByVal compareRange5 As Range, Optional ByVal Criteria5 As Variant, _
                            Optional NoDuplicates As Boolean) As String
 
Upvote 0

Forum statistics

Threads
1,223,992
Messages
6,175,831
Members
452,673
Latest member
LaMiaAvy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top