2

I have a column of cells. They need to be concatenated into a single string with multiple delimiters in between.

I need something like the result in column 2 from the values in column 1

Column 1 | column 2
a1       |   'a1'
a2       |    'a1';'a2'
a3       |    'a1';'a2';'a3'
a4       |    'a1';'a2';'a3';'a4'
a5       |    'a1';'a2';'a3';'a4';'a5'

Currently I use the following formula

Column 1  |   Column 2
a1        |  ="'"&a1&"'"&";"
a2        |  =b1&"'"&a2&"'"&";"
a3        |

and copy it down the column B2.

Is there a VBA code that could help me do that. I did find some that would add a single delimiter ' between the cells but I could not modify it to add multiple delimiters.

It would be very helpful could share a VBA code for the same.

  • 2
    Yes there probably is. – JNevill Mar 24 '17 at 17:25
  • 1
    If you are using Excel 2016, the `TEXTJOIN` function is all you would need – CallumDA Mar 24 '17 at 17:29
  • Possible duplicate of [How to add single quote and comma to all values in a column?](http://stackoverflow.com/questions/42915255/how-to-add-single-quote-and-comma-to-all-values-in-a-column) – CallumDA Mar 24 '17 at 17:30
  • Just asked and answered three times at [Excel VBA UDF for concatenating is giving an Error message](http://stackoverflow.com/questions/43004691/excel-vba-udf-for-concatenating-is-giving-an-error-message/43005063#43005063). –  Mar 24 '17 at 18:33
  • Thanks everyone for all your help. I will try all these methods and let you know which worked the best for me. Thanks again for everyone for all your help. – Shrilakshmi Bartur Mar 27 '17 at 17:05

6 Answers6

4

You can use the Join method in VBA with a given delimeter:

e.g.

someArray = Array("some", "words", "here")
Debug.Print "'" & Join(someArray, "';'") & "'"

'// will print:
'// 'some';'words';'here'
SierraOscar
  • 17,507
  • 6
  • 40
  • 68
3

I know you asked for VBA, but you can do this with a formula:

In B2:

=IF(ROW()=2,TEXTJOIN(";",TRUE,"'"&B1,"'"&A2&"'"),TEXTJOIN(";",TRUE,B1,"'"&A2&"'"))

and drag down:

enter image description here

Or

In B1, use ="'"&A1&"'", then in B2 (and drag down):

=SUBSTITUTE(TEXTJOIN(";",TRUE,"'"&B1,"'"&A2&"'"),"'","",1)
BruceWayne
  • 22,923
  • 15
  • 65
  • 110
2

You can achieve desired output using this formula:

=CONCATENATE(B2, " ; '", A3, "'")

First put value from cell A2 to B2 manually (using ="'" & A2 & "'" formula) and then paste this formula in cell B3 and drag it down.

Here is the result with updated formula:

enter image description here

UPDATE (Thanks to BruceWayne)

Enter the value in cell B2 using:

="'" & A2 & "'"

So it will take the first '

ManishChristian
  • 3,759
  • 3
  • 22
  • 50
1

I would just do a simple loop like so.

Sub combineRows()

    'start and end rows, assuming column A
    Dim startRow, endRow As Integer

    Dim myString, myAdd As String

    startRow = 2
    endRow = 6



    For i = startRow To endRow


        myAdd = "'" & Range("A" & i) & "'" & ";"

        myString = myString + myAdd

        Range("B" & i) = myString

    Next i



End Sub
Robomato
  • 257
  • 1
  • 2
  • 13
  • Hi, Thanks for this simple code. It is pretty easy. However I am having two problems. 1. The first ' is missing for the first entry in all rows 2. A ";" is remaining after the last entry in all rows Can you please help me with this is issue? Why is it happening? Can it be fixed? Thanks – Shrilakshmi Bartur Apr 02 '17 at 17:36
1

Here's my JoinRange function. It has a few more options than you're looking for.

Public Function JoinRange(rInput As Range, _
     Optional sDelim As String = vbNullString, _
     Optional sLineStart As String = vbNullString, _
     Optional sLineEnd As String = vbNullString, _
     Optional sBlank As String = vbNullString, _
     Optional sQuotes As String = vbNullString, _
     Optional IgnoreBlanks As Boolean = True) As String

     Dim vaCells As Variant
     Dim i As Long, j As Long
     Dim lCnt As Long
     Dim aReturn() As String

     If rInput.Cells.Count = 1 Then
        ReDim aReturn(1 To 1)
        aReturn(1) = sQuotes & rInput.Value & sQuotes
     Else
        vaCells = rInput.Value
         ReDim aReturn(1 To rInput.Cells.Count)

         For i = LBound(vaCells, 1) To UBound(vaCells, 1)
             For j = LBound(vaCells, 2) To UBound(vaCells, 2)
                 If Len(vaCells(i, j)) = 0 Then
                     If Not IgnoreBlanks Then
                         lCnt = lCnt + 1
                         aReturn(lCnt) = sQuotes & sBlank & sQuotes
                     End If
                 Else
                     lCnt = lCnt + 1
                     aReturn(lCnt) = sQuotes & vaCells(i, j) & sQuotes
                 End If
             Next j
         Next i

         ReDim Preserve aReturn(1 To lCnt)
     End If

     JoinRange = sLineStart & Join(aReturn, sDelim) & sLineEnd

End Function

use it in B1 like

=JoinRange($A$1:A1,";")

and fill down.

Dick Kusleika
  • 32,673
  • 4
  • 52
  • 73
0

For the sake of avoiding visual confusion, I will offer CHAR(39)&CHAR(59)&CHAR(39) in place of "';'".

In B1, use this:

=CHAR(39)&TEXTJOIN(CHAR(39)&CHAR(59)&CHAR(39), TRUE, A$1:A1)&CHAR(39)

Fill down.

![enter image description here

... or if the end result is the only important thing,

=CHAR(39)&TEXTJOIN(CHAR(39)&CHAR(59)&CHAR(39), TRUE, A1:A5)&CHAR(39)

enter image description here

A single tick (aka single quote or ') is ASCII character 39 and a semi-colon is ASCII character 59.

Community
  • 1
  • 1