Adding a comma after every 9 characters

grussell070769

New Member
Joined
Sep 12, 2022
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
I need to add a comma after every nine characters that would yield a very large return.

Example: Current Chain of numbers
144849383600226994968816910897204120078656326273603444077371702

Need:
144849383,600226994,968816910,897204120,078656326,273603444,077371702,
 
I only have XL365, so I cannot test this (can't be sure if dynamic arrays are why it works for me or not). In any event, this one-liner function is only for the OP's original question (if it works) and not for a general case (it should handle up to 100 digits)...
VBA Code:
Function AddCommas(S As String) As String
  AddCommas = Replace(Application.Trim(Join(Evaluate("MID(""" & S & """,{1,10,19,28,37,46,55,64,73,82,91},9)&"" """))), " ", ",")
End Function
 
Last edited:
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Peter,

I cannot remove the last comma [","], (the one on the right).
Can you help me?
I deliberately made sure that last comma was there because it was there in the "Need" string in post #1.
Yours is a different question but it would just require a small change to the pattern line.

Rich (BB code):
Function AddCommas(s As String, Optional Where As Long = 9, Optional Delim As String = ",") As String
  With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "(.{" & Where & "})(?!$)"
    AddCommas = .Replace(s, "$1" & Delim)
  End With
End Function

grussell070769.xlsm
AB
1144849383600226994968816910897204120078656326273603444077371702144849383,600226994,968816910,897204120,078656326,273603444,077371702
Add commas
Cell Formulas
RangeFormula
B1B1=AddCommas(A1)
 
Upvote 0

Peter_SSs

I deliberately made sure that last comma was there because it was there in the "Need" string in post #1.
Yes, I know the OP's request.

Peter_SSs

Yours is a different question but it would just require a small change to the pattern line.
I know. I tried to modify the code, but I didn't succeed.
Thank you very much.
 
Upvote 0
I modified the one-liner that I posted in Message #21 so that the user can specify the count before a delimiter is applied and what that delimiter is. I used Peter's function header with the Optional values pre-assigned in accordance with the OP original request. NOTE - this function does not include a trailing delimiter at the end of the text it returns (if one is needed, it can always be concatenated on the end of the function within the formula that calls it). Also, this function should be able to process a much longer text value than my original function did. And I still do not know if the dynamic array feature of my XL365 is why the code is working for me (that is, I cannot test if this will work in lower versions of Excel or not)... if it does not work in a lower version when committing the formula with Enter, try committing the formula with CTRL+SHIFT+ENTER and see if that makes it work.
VBA Code:
Function AddCommas(S As String, Optional Where As Long = 9, Optional Delim As String = ",")
  AddCommas = Replace(Application.Trim(Join(Evaluate("MID(""" & S & """,COLUMN(A:ZZ)*" & Where & "-" & Where & "+1," & Where & ")&"" """), "")), " ", Delim)
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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