0

I have two excel cells(A1 and B1) each contains comma delimited values in, I want to combine these values into one cell (C1), so that the first value from cell A1 is "linked" to the first value from cell B1 by a comma and so on for all the values For example: Cell A1 contains the following: B21:01,B22:02,B23:01,B25:01 Cell B1 contains the following: 1578,2758,10599,5478 Cell C1 should look like this: B21:01,1578,B22:02,2758,B23:01,10599,B25:01,5478

I have tried =concatenate(transpose(A1:A4)&",") which works well for 1 column, (if I transpose the values into a column) but cannot get it to work across multiple columns. Is there an easy way to do this?

2 Answers 2

2

Use this formula:

=TEXTJOIN(",",0,TRANSPOSE(TEXTSPLIT(A1&";"&B1,",",";")))

This is how it works:

Assuming A1 contains A,B,C,D,E and B1 contains 10,20,30,40,50:

  1. A1 & ";" & B1 concatenates them, adding ; between them: A,B,C,D,E;10,20,30,40,50

  2. TEXTSPLIT((that),",",";") expands them into a table:

    A B C D E
    10 20 30 40 50
  3. TRANSPOSE((that)) transposes that table:

    A 10
    B 20
    C 30
    D 40
    E 50
  4. TEXTJOIN(",",0,(that)) goes row-by-row, cell-by-cell over that table and joins up the contents of the cells, inserting , between them: A,10,B,20,C,30,D,40,E,50

0

Are you willing to use VBA? If so, this function will do what you want:

Function pair(ByVal c1 As Range, ByVal c2 As Range, ByVal separator As String) As String
    Dim arr1 As Variant, arr2 As Variant
    arr1 = Split(c1.Value, separator)
    arr2 = Split(c2.Value, separator)
    
    For i = 0 To WorksheetFunction.Min(UBound(arr1), UBound(arr2))
        pair = pair & arr1(i) & separator & arr2(i) & separator
    Next i
    pair = Left(pair, Len(pair) - Len(separator))
End Function

You can use it in your workbook like so (just replace the semicolons with commas. My language settings use semicolons) :

enter image description here

edit: This function will take the smaller number of elements between the two cells. So if there are 4 values in cell 1 and 3 values in cell 2, it will only make 3 pairs and ignore the 4th value in cell 1. Depending on whether or not this is desirable for you, you might need to modify the function.

edit2: and here is a version which strips decimals from the values in second cell:

Function pair(ByVal c1 As range, ByVal c2 As range, ByVal separator As String) As String
    Dim arr1 As Variant, arr2 As Variant
    arr1 = Split(c1.Value, separator)
    arr2 = Split(c2.Value, separator)
    
    For i = 0 To WorksheetFunction.Min(UBound(arr1), UBound(arr2))
        pair = pair & arr1(i) & separator & Split(arr2(i), ".")(0) & separator
    Next i
    pair = Left(pair, Len(pair) - Len(separator))
End Function
2
  • Thanks for your help. Is there a way to remove decimals from a csv string. The second set of numbers have decimals eg B21:01,1578.01,B22:02,2758.54 which I want to remove. The numbers come across into excel as a CSV string and I would like to either remove them prior to running the pair function or after which ever is easier, if it is possible at all.
    – James
    Commented Jan 31, 2023 at 13:03
  • @James I added another version of the function to the answer. This version will cut the decimals off. Let me know if it works, thanks!
    – andrewb
    Commented Jan 31, 2023 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .