Adding a comma between single digits in a cell

lind33

New Member
Joined
Jun 25, 2019
Messages
7
I am looking for a way to add a comma after every single digit in a cell.
I found some formulas on the web but since the number of digits is not equal in the cells, I can't get the result I need.

For example, in column A:
12345
12
135789
135
etc.

What formula can give me the result I need? can I get some help, please? thanks.
Expected results of the above example:
1,2,3,4,5
1,2
1,3,5,7,8,9
1,3,5
 

Some videos you may like

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
9,922
Try:

Code:
=MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,0,",0"),1,",1"),2,",2"),3,",3"),4,",4"),5,",5"),6,",6"),7,",7"),8,",8"),9,",9"),2,999)
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,200
This macro will place the results in column B.
Code:
Sub lind33()
    Dim i As Long, rng As Range, RngList As Object
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Set RngList = CreateObject("Scripting.Dictionary")
    For Each rng In Sheets("Sheet1").Range("A1:A" & LastRow)
        For i = 1 To Len(rng)
            RngList.Add Mid(rng, i, 1), Nothing
        Next i
        rng.Offset(0, 1) = Join(RngList.keys, ", ")
        RngList.RemoveAll
    Next rng
End Sub
 

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
9,922
Yeah, it is quite a formula, isn't it?! :rolleyes:

If you have Excel 365 with the TEXTJOIN function, you can simplify it to:

=TEXTJOIN(",",1,MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))

confirmed with Control+Shift+Enter. But I didn't want to assume you had that. In any event, glad we could help! :)
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,059
Office Version
2010
Platform
Windows
This macro will place the results in column B.
Code:
Sub lind33()
    Dim i As Long, rng As Range, RngList As Object
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Set RngList = CreateObject("Scripting.Dictionary")
    For Each rng In Sheets("Sheet1").Range("A1:A" & LastRow)
        For i = 1 To Len(rng)
            RngList.Add Mid(rng, i, 1), Nothing
        Next i
        rng.Offset(0, 1) = Join(RngList.keys, ", ")
        RngList.RemoveAll
    Next rng
End Sub
Your macro can be written using only one loop...
Code:
Sub AddCommasBetweenDigits()
  Dim X As Long, LastRow As Long, Cell As Range
  LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  For Each Cell In Sheets("Sheet2").Range("A1:A" & LastRow)
    Cell.Offset(, 1) = Replace(StrConv(Cell.Value, vbUnicode), Chr(0), ",", , Len(Cell.Value) - 1)
  Next
End Sub
HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (AddCommasBetweenDigits) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Last edited:

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,230
Office Version
2007
Platform
Windows
Try whit UDF (User Definition Function)

Code:
Function AddComma(s As Range)
    Dim i As Long, cad As String
    For i = 1 To Len(s)
        cad = cad & Mid(s, i, 1) & ","
    Next
    AddComma = Left(cad, Len(cad) - 1)
End Function
Use like formula excel:

=AddComma(A1)
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,059
Office Version
2010
Platform
Windows
Yeah, it is quite a formula, isn't it?! :rolleyes:

If you have Excel 365 with the TEXTJOIN function, you can simplify it to:

=TEXTJOIN(",",1,MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))

confirmed with Control+Shift+Enter. But I didn't want to assume you had that. In any event, glad we could help! :)
Actually Eric, this can be done with a "simple" normally-entered formula in any version of Excel (that I am familiar with)...

=MID(TEXT(A1,REPT("\,0",LEN(A1))),2,2*LEN(A1))
 
Last edited:

lind33

New Member
Joined
Jun 25, 2019
Messages
7
Thank you, guys all, for your great answers really. I appreciate it.
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,059
Office Version
2010
Platform
Windows
Try whit UDF (User Definition Function)

Code:
Function AddComma(s As Range)
    Dim i As Long, cad As String
    For i = 1 To Len(s)
        cad = cad & Mid(s, i, 1) & ","
    Next
    AddComma = Left(cad, Len(cad) - 1)
End Function
Use like formula excel:

=AddComma(A1)
Given the formula I posted in Message #8 , a UDF for this functionality (if one was thought to be necessary now) could be written as a non-looping one-liner...
Code:
Function AddCommas(s As String) As String
  AddCommas = Evaluate(Replace("MID(TEXT(@,REPT(""\,0"",LEN(@))),2,2*LEN(@))", "@", s))
End Function
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,102,872
Messages
5,489,408
Members
407,687
Latest member
NeoSez

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top