Combining cells in one column based on values in cells of another column

angel34

Board Regular
Joined
Jun 3, 2016
Messages
79
Seasons greetings.

I have a table in excel where column A consists of dates in unsorted manner. Same dates may appear several times in the column. Column B comprises of text values. I would like to combine (concatenate), the values in Column B with a comma separator, where date in Column A is identical. As for example, for all date entries of 1/10/2018, if there are 4 entries altogether (say) in A1, A2, A3 and A4, I am looking to combine the corresponding texts in B1, B2, B3 and B4 in Column E.

I have already extracted the unique date values in Column D and looking for a formula in Column E which will combine the text values in B1:B4 based on the unique date values extracted in Column D.

I was trying to use the formula CONCATENATEIF(A:A,D1,B:B,",") but soon realized that this is not a standalone excel formula and should be used with a UDF to make it work.

I am neither able to use a UDF or a helper column and soI was wondering whether there could be a solution to this by using a standard excel formula which can be copied down Column E to accomplish the task.

I shall be most thankful for any insight. Thanks and Regards
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
"I am neither able to use a UDF or a helper column"

I presume this statement means you can only use a formula and not that you dont have knowledge of UDFs/VBA or helper columns ?

To be honest I think the only solution would be a UDF/VBA of some sort.
 
Upvote 0
{=IF($A1:$A5=$A1,TEXTJOIN(", ",1,IF(EXACT($A1:$A5,$A1),$B1:$B5,"")),"")}

It's an array formula so Ctrl+Shift+Enter after paste.
 
Upvote 0
Your presumption is correct Special K99. I am unable to use UDF or a helper column because the file gets transmitted to other users downstream who may not be tech savvy and there is a chance of the helper column being inadvertently overwritten. My apologies for not being clear. Thank you for the reply.
Thanks for the reply Flashbond. Unfortunately, I could not make the formula work. I pressed Ctrl+Shift+Enter while pasting and the two curly brackets appeared as usual indicating that the pasting is correct. But there is no value in the cell and the formula itself can be seen in the cell. I use excel 2007. Any ideas what mistake I may be making?
No error, whatsoever, is displayed.Thanks
 
Last edited:
Upvote 0
I limited the range until 5th row. You may alter them like A:A or B:B. Other posibility may be you dont have TEXTJOIN if you are running an older version of Excel. Then you must use VBA for string concanate.
 
Upvote 0
Thank you once again for your reply. I adjusted the range to suit my needs. I shall definitely explore the possibilities of using vba or UDF and revert back.
 
Upvote 0
Thank you once again for your reply. I adjusted the range to suit my needs. I shall definitely explore the possibilities of using vba or UDF and revert back.

Ok try:
A B
1 a
2 b
3 c
4 d
1 e
1 f

If you paste my code to C1 it will produce: "a, e, f".
You should modify it for your needs.
 
Upvote 0
I did exactly what you asked me to do. This time on a new sheet, without any modification to the range. It throws a #NAME ? error.
 
Upvote 0
Ok then your Excel doesn't have TEXTJOIN.


Try:[FONT=&quot]
[/FONT]=IF(AND(NOT(EXACT($A$1:A1,A2))),StringConcat(", ",IF(EXACT($A$2:$A$9,A2),$B$2:$B$9,"")),"")

[FONT=&quot]Have the following [/FONT]StringConcat [FONT=&quot]function:
VBA:
[/FONT]
Code:
Function StringConcat(Sep As String, ParamArray Args()) As Variant
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' StringConcat
' By Chip Pearson, chip@cpearson.com, www.cpearson.com
'                  www.cpearson.com/Excel/stringconcatenation.aspx
' This function concatenates all the elements in the Args array,
' delimited by the Sep character, into a single string. This function
' can be used in an array formula. There is a VBA imposed limit that
' a string in a passed in array (e.g.,  calling this function from
' an array formula in a worksheet cell) must be less than 256 characters.
' See the comments at STRING TOO LONG HANDLING for details.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim S As String
Dim N As Long
Dim M As Long
Dim R As Range
Dim NumDims As Long
Dim LB As Long
Dim IsArrayAlloc As Boolean


'''''''''''''''''''''''''''''''''''''''''''
' If no parameters were passed in, return
' vbNullString.
'''''''''''''''''''''''''''''''''''''''''''
If UBound(Args) - LBound(Args) + 1 = 0 Then
    StringConcat = vbNullString
    Exit Function
End If


For N = LBound(Args) To UBound(Args)
    ''''''''''''''''''''''''''''''''''''''''''''''''
    ' Loop through the Args
    ''''''''''''''''''''''''''''''''''''''''''''''''
    If IsObject(Args(N)) = True Then
        '''''''''''''''''''''''''''''''''''''
        ' OBJECT
        ' If we have an object, ensure it
        ' it a Range. The Range object
        ' is the only type of object we'll
        ' work with. Anything else causes
        ' a [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=VALUE]#VALUE[/URL]  error.
        ''''''''''''''''''''''''''''''''''''
        If TypeOf Args(N) Is Excel.Range Then
            '''''''''''''''''''''''''''''''''''''''''
            ' If it is a Range, loop through the
            ' cells and create append the elements
            ' to the string S.
            '''''''''''''''''''''''''''''''''''''''''
            For Each R In Args(N).Cells
                If Len(R.Text) > 0 Then
                    S = S & R.Text & Sep
                End If
            Next R
        Else
            '''''''''''''''''''''''''''''''''
            ' Unsupported object type. Return
            ' a [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=VALUE]#VALUE[/URL]  error.
            '''''''''''''''''''''''''''''''''
            StringConcat = CVErr(xlErrValue)
            Exit Function
        End If
    
    ElseIf IsArray(Args(N)) = True Then
        '''''''''''''''''''''''''''''''''''''
        ' ARRAY
        ' If Args(N) is an array, ensure it
        ' is an allocated array.
        '''''''''''''''''''''''''''''''''''''
        IsArrayAlloc = (Not IsError(LBound(Args(N))) And _
            (LBound(Args(N)) <= UBound(Args(N))))
        If IsArrayAlloc = True Then
            ''''''''''''''''''''''''''''''''''''
            ' The array is allocated. Determine
            ' the number of dimensions of the
            ' array.
            '''''''''''''''''''''''''''''''''''''
            NumDims = 1
            On Error Resume Next
            Err.Clear
            NumDims = 1
            Do Until Err.Number <> 0
                LB = LBound(Args(N), NumDims)
                If Err.Number = 0 Then
                    NumDims = NumDims + 1
                Else
                    NumDims = NumDims - 1
                End If
            Loop
            On Error GoTo 0
            Err.Clear
            ''''''''''''''''''''''''''''''''''
            ' The array must have either
            ' one or two dimensions. Greater
            ' that two caues a [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=VALUE]#VALUE[/URL]  error.
            ''''''''''''''''''''''''''''''''''
            If NumDims > 2 Then
                StringConcat = CVErr(xlErrValue)
                Exit Function
            End If
            If NumDims = 1 Then
                For M = LBound(Args(N)) To UBound(Args(N))
                    If Args(N)(M) <> vbNullString Then
                        S = S & Args(N)(M) & Sep
                    End If
                Next M
                
            Else
                ''''''''''''''''''''''''''''''''''''''''''''''''
                ' STRING TOO LONG HANDLING
                ' Here, the error handler must be set to either
                '   On Error GoTo ContinueLoop
                '   or
                '   On Error GoTo ErrH
                ' If you use ErrH, then any error, including
                ' a string too long error, will cause the function
                ' to return [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=VALUE]#VALUE[/URL]  and quit. If you use ContinueLoop,
                ' the problematic value is ignored and not included
                ' in the result, and the result is the concatenation
                ' of all non-error values in the input. This code is
                ' used in the case that an input string is longer than
                ' 255 characters.
                ''''''''''''''''''''''''''''''''''''''''''''''''
                On Error GoTo ContinueLoop
                'On Error GoTo ErrH
                Err.Clear
                For M = LBound(Args(N), 1) To UBound(Args(N), 1)
                    If Args(N)(M, 1) <> vbNullString Then
                        S = S & Args(N)(M, 1) & Sep
                    End If
                Next M
                Err.Clear
                M = LBound(Args(N), 2)
                If Err.Number = 0 Then
                    For M = LBound(Args(N), 2) To UBound(Args(N), 2)
                        If Args(N)(M, 2) <> vbNullString Then
                            S = S & Args(N)(M, 2) & Sep
                        End If
                    Next M
                End If
                On Error GoTo ErrH:
            End If
        Else
            If Args(N) <> vbNullString Then
                S = S & Args(N) & Sep
            End If
        End If
        Else
        On Error Resume Next
        If Args(N) <> vbNullString Then
            S = S & Args(N) & Sep
        End If
        On Error GoTo 0
    End If
ContinueLoop:
Next N


'''''''''''''''''''''''''''''
' Remove the trailing Sep
'''''''''''''''''''''''''''''
If Len(Sep) > 0 Then
    If Len(S) > 0 Then
        S = Left(S, Len(S) - Len(Sep))
    End If
End If


StringConcat = S
'''''''''''''''''''''''''''''
' Success. Get out.
'''''''''''''''''''''''''''''
Exit Function
ErrH:
'''''''''''''''''''''''''''''
' Error. Return [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=VALUE]#VALUE[/URL] 
'''''''''''''''''''''''''''''
StringConcat = CVErr(xlErrValue)
End Function
Check for further information: https://www.google.com.tr/amp/s/amp.reddit.com/r/excel/comments/75iiyr/concat_cells_if_same_value_exists_in_another/
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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