Separating characters by space

kishorkhanal

Active Member
Joined
Mar 23, 2006
Messages
434
I have following kinds of texts in a column.

MISCELLANEOUS
EQUIPMENT
JBKU348597-3
T-225
75984

I want to convert above texts with space between every character as follows:

M I S C E L L A N E O U S
E Q U I P M E N T
J B K U 3 4 8 5 9 7 - 3
T - 2 2 5
7 5 9 8 4

Please help!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Put this is a Standard Module - It;s a UDF - It works!!

With you example above statrting in A1 and downward to A5 -- enter into
Cell B1 =Extraspaces(A1)
and copy down


Function ExtraSpaces(Rng As Range)
Dim a As String
Dim i As Integer
Dim t As String
For i = 1 To Len(Rng)
t = Mid(Rng, i, 1)
If i > 1 Then
a = a & " " & t
Else
a = t
End If
Next i
ExtraSpaces = a
End Function

HTH
 
Upvote 0
Hi

There are a couple of ways of doing this. You could either use a self-referencing formula that uses iterations or a user defined function in VBA.

A UDF in VBA (insert into a new standard module) might look something like this:
Code:
Function addSpace(inputStr As String) As String

Dim i As Long
addSpace = ""

For i = 1 To Len(inputStr)
    addSpace = addSpace & Mid$(inputStr, i, 1) & " "
Next i

addSpace = RTrim(addSpace)

End Function

You then call that function by entering the following formula:
=AddSpace(A1)

If you would rather use a formula, then you can do this with a self-referencing formula as follows:
1) Select menu option Tools > Options > Calculation > set Iteration to 10
2) Enter the following formula into cell B1:
Code:
=IF(B1=0, "", B1 & MID(A1, 1+LEN(B1)/2, 1) & " ")
3) Copy this formula down as far as you want.
4) Press and hold the F9 key until all of the changes are completed.
5) You can now copy > paste special the values in column B and remove the calculation iteration.

Please note that if you complete the calculation before copying down the formula then it won't work as expected, and each formula below the first will need to be re-pasted.

Andrew
 
Upvote 0
I have put the pattern of . in a group which is what I am using in the Replace, the $1 representing group 1, so it is going to replace in r the value of the group followed by a space. Since the pattern in the group is any character . and Global is set to true, it will replace any character (the regular expression) with that character followed by a space.
 
Upvote 0
Thanks for that. So the '$1' represents the group string that matches the pattern (in this case a single character) and the character(s) before the " is what gets appended between each group......right. So you could use dots or slashes or any other character......

It's quite a bit slower isn't it?

Andrew
 
Upvote 0
I think it's faster than looping through the string.


.Replace(r," ")

If our replace looked like that above (and assuming global still set to true), it would replace everything that matched the pattern with a space.

but this

.Replace(r,"$1 ")

This however references the pattern from the regular expression and replaces in the string the value(s) it finds followed by a space, but yes you could use other characters.

You can also use this technique with multiple groups, a common example is this:

TheBlueCar

Here you want to put a space between lowercase letters and capital letters, so we will use two groups:

Code:
Sub TwoGroups()
Dim r As String
r = "TheBlueCar"
With CreateObject("vbscript.regexp")
    .Pattern = "([a-z])([A-Z])"
    .Global = True
    MsgBox .Replace(r, "$1 $2")
End With
End Sub

The first match is eB, but we don't want to replace the e and the B, we want to keep them, so we need to reference those groups in the Replace, so it will replace the eB with e B
 
Last edited:
Upvote 0
I think it's faster than looping through the string.
I did a quick test using 20k rows of data comparing my example UDF to the regexp UDF. The test was done along the lines of that outlined in the guru book, and the rows of data contained random text of between 1 and 5 characters each. My UDF took less than a second while the regexp UDF took 20 times longer. I suspect the issue with larger data sets is the 'create object' line.....however, if it were programmed more efficiently (maybe as a sub rather than UDF) I would expect the regexp to be faster.

Cheers
Andrew
 
Upvote 0
Use global variable
Code:
Private regX As Object
 
Function addspace(ByVal r As String) As String
If RegX Is Nothing Then Set regX = CreateObject("VBScript.RegExp")
With regX
    .Pattern = "(.)"
    .Global = True
    addspace = RTrim(.Replace(r, "$1 "))
End With
End Function
 
Upvote 0

Forum statistics

Threads
1,224,895
Messages
6,181,619
Members
453,057
Latest member
LE102024

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