Separate cell value into user specified size chunks with space seperator. How to?

JugglerJAF

Active Member
Joined
Feb 17, 2002
Messages
297
Office Version
  1. 365
Platform
  1. Windows
I'm looking for a custom function that will take a long character string (for example: 1234ABCD5678EFGH in cell A1), and split it into chunks in column B, and have each chunk separated by spaces (but all in the same output column) with the number of characters in each chunk to be specified by the user.

For example, if the chunk size was 4, then the B1 would be "1234 ABCD 5678 EFGH", but if the chunk size was 7 then the B1 would be "1234ABC D5678EF GH".

The length of the initial text string could vary and as you can see in the "7" example above, it's not always going to divide into equal sized chunks.

I've been scratching my head over how to do this. I'm sure there is a way, but I can't think of it.

Any suggestions?
 
try this


Excel 2010
C
11234ABCD5678EFGH
21234 ABCD 5678 EFGH
Sheet7
Cell Formulas
RangeFormula
C2=sep(C1,4)


Code:
Function Sep(S As String, chunk As Integer) As String
Dim tmpStr As String
Do Until chunk > Len(S)
    tmpStr = tmpStr & Left(S, chunk) & " "
    S = Mid(S, chunk + 1)
Loop

If Len(S) Then
    tmpStr = tmpStr & S
Else
    tmpStr = Left(tmpStr, Len(tmpStr) - 1)
End If
Sep = tmpStr
End Function


try this
 
Last edited:
Upvote 0

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