Change Spaces to"_"

irresistible007

Board Regular
Joined
Nov 24, 2005
Messages
173
I have a text box, list Box and a cmd Button... clicking cmd button will add the Item typed in text box to List Box... BUT!!! i need to change any spaces to underscores, for instance if somebody typed "United Kingdom" and hit the add button then the item should appear like "United_Kingdom" in the List Box....

Any suggestions... though I know that the Chr function is the main ingredient but then how to look for positions and repeatitions?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
If you want to replace all spaces with underscores, try:

sItem = Replace(Trim(sItem), " ", "_")

Where sItem is the data from the textbox

You can then add sItem to the listbox.

Denis
 

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
You could, but it's unnecessary. Replace does the job very efficiently by itself.

I added Trim in case anyone typed a space at the beginning or end.

Denis
 

Watch MrExcel Video

Forum statistics

Threads
1,133,460
Messages
5,658,899
Members
418,475
Latest member
ExcelBeginner233

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
Top