Trim / Substitute / Replace / Len - replacing excess spaces with a comma and space - read on!

manc

Active Member
Joined
Feb 26, 2010
Messages
340
Good day Demigods,

Sheet 1, cell A1 contains alphanumeric user entered data and is referenced on ten other sheets.
Some users enter data as
"E022563, E055634, E074596"
,
Others enter data as, for example,
"E002563", then 5 spaces, "E055634", then 4 spaces, "E074596"
- curse them!

I'm not bothered how it looks on Sheet 1, cell A1, but for the other worksheets, I want the data the user has entered to be displayed as
"E022563, E055634, E074596"
- one comma followed by a space.

I can trim the excess spaces, but don't know how to add the comma.

Any help/comments greatly appreciated, as usual.
Best Regards
manc
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Maybe this:

Code:
=""""&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," ",""),",,",", "),"""","")&""""

A small modification

=""""&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," ",""),",,",","),",",", "),"""","")&""""

Markmzz
 
Last edited:
Upvote 0
In an empty cell in sheet1 enter:
=IF(ISNUMBER(SEARCH(",",A1)),TRIM(A1),SUBSTITUTE(TRIM(A1)," ",", "))
and reference this cell instead of A1. This should handle no commas, and excess spaces, but doesn't guard against an entry that is missing some but not all of the commas you want.
 
Upvote 0
Another modification:

Code:
=""""&SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,","," ")),"""","")," ",", ")&""""

Markmzz
 
Upvote 0
Dear Markmzz and JoeMo,

Thanks very much for your prompt suggestions.
Both work great, although there's less room for user error using markmzz's suggestion.

Thank-you both for your time.
Best Regards
manc
 
Upvote 0

Forum statistics

Threads
1,215,346
Messages
6,124,417
Members
449,157
Latest member
mytux

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