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!
 
Out of curiosity, can you try the test I tried? I used the following code in a workbook module (change 'Sheet6' to suit):

Code:
Sub testSpeed()

Application.ScreenUpdating = False

With Sheets("Sheet6")
    .Range("A1:A20000").Formula = "=randbetween(-10000000,10000000)"
    .Range("A1:A20000").Value = .Range("A1:A20000").Value

    starttime = Timer
    .Range("B1:B20000").FormulaR1C1 = "=addspace(RC1)"
    Application.Calculate
    .Range("B1:B20000").Value = .Range("B1:B20000").Value
    elap1 = Timer - starttime
    
    .Range("B1:B20000").FormulaR1C1 = "=addspace2(RC1)"
    Application.Calculate
    .Range("B1:B20000").Value = .Range("B1:B20000").Value
    elap2 = Timer - starttime
    
End With

Application.ScreenUpdating = True

MsgBox "UDF takes " & elap1 & vbCr & "regexp takes " & elap2

End Sub

and the following code in a standard module:

Code:
Private regX As Object

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


Function addspace2(r As String) As String

If regX Is Nothing Then Set regX = CreateObject("VBScript.RegExp")

With CreateObject("vbscript.regexp")
    .Pattern = "(.)"
    .Global = True
    addspace2 = Trim(.Replace(r, "$1 "))
End With

End Function

Since the variable declaration the gap has improved, but I'm seeing a much better performance from the UDF that doesn't use regexp.......

Andrew
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Yes of-course.

Using the global variable is only reducing the access time to RegExp.

The built-in function should be faster.
 
Upvote 0
Yes of-course.

Using the global variable is only reducing the access time to RegExp.

The built-in function should be faster.
Which one - the regexp? I'm just not seeing it.......

My mistake with the previous code, the amended code should be :
Code:
Function addspace2(r As String) As String

If regX Is Nothing Then Set regX = CreateObject("VBScript.RegExp")

With regX
    .Pattern = "(.)"
    .Global = True
    addspace2 = Trim(.Replace(r, "$1 "))
End With

End Function

Andrew
 
Upvote 0
What I meant was

1) UDF using global variable variable, regX, is faster than the UDF accessing each individual UDF.(With CreateObject(....thingy))

2) Your UDF, simply looping through the variable is the fastest.
 
Upvote 0

Forum statistics

Threads
1,224,915
Messages
6,181,726
Members
453,064
Latest member
robatthe2A

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