Adding Spaces Between Characters

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello all, I want to add spaces between the characters in the string. The macro should add space between each character.If already space is exist it should ignore and add only where space is required. The string is in last used row of H column. Thank you.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
how often are you doing that. a quick find replace should suffice, and that could be recorded
 
Upvote 0
Code:
[color=darkblue]Sub[/color] Insert_Spaces()
    [color=darkblue]Dim[/color] str1 [color=darkblue]As[/color] [color=darkblue]String[/color], str2 [color=darkblue]As[/color] [color=darkblue]String[/color], i [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]With[/color] Range("H" & Rows.Count).End(xlUp)
        str1 = .Value
        [color=darkblue]For[/color] i = 1 [color=darkblue]To[/color] Len(str1)
            str2 = str2 & Mid(str1, i, 1) & " "
        [color=darkblue]Next[/color] i
        .Value = Application.Trim(str2)
    [color=darkblue]End[/color] [color=darkblue]With[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Assuming normal ASCII characters (with ASCII codes less than 256), then here is another macro that you might want to consider (it does not use any loops)...
Code:
[table="width: 500"]
[tr]
	[td]Sub InsertSpacesBetweenCharacters()
  With Cells(Rows.Count, "H").End(xlUp)
    .Value = Application.Trim(Replace(StrConv(.Value, vbUnicode), Chr(0), " "))
  End With
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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