replace Blank Spaces in text string by inserting a comma where there are 2 or more spaces.

blbat

Active Member
Joined
Mar 24, 2010
Messages
338
Office Version
  1. 2016
  2. 2013
I have many cells with text strings that may contain 2, or more, blank spaces within that cells text. (I don't care about a blank space of 1). each cell may contain multiple instances of these blank spaces. There is no order to the blank spaces, so i need to find the blank spaces and insert a comma.

My end goal is to do a "text to columns" type of function to separate the individual strings of text.

so a cell A1 that contains:
Code:
txt sometext                       text and number-98            20b)1            G 4.1.04.1.0.3            Sep-17              STUFF                  SW 1        Location          GL morestuff                        year2020

would look like:

Code:
txt sometext,text and number-98,20b)1,G 4.1.04.1.0.3,Sep-17,STUFF,SW 1,Location,GL morestuff,year2020,


Maybe some combination of LEN and SUBSTITUTE?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
maybe Find and Replace:

Find what: " " (two spaces)
Replace with: "," (single comma)

then again:
Find what: ",,"
Replace with: ","

no quotes
 
Upvote 0
If I use "find and replace" from tool bar I end up with Multiple commas:

Code:
sometext,,,,,,,,,,, text and number-98,,,,,,20b)1,,,,,,G 4.1.04.1.0.3,,,,,,Sep-17,,,,,,,STUFF,,,,,,,,,SW 1,,,,Location,,,,,GL morestuff,,,,,,,,,,,,year2020,,,,,,

that's why I'm looking for a way to get a single comma.
 
Upvote 0
did you read my whole post ?

second step try few times to see answer: We couldn't find anything to replace... etc bla bla

txt sometext, text and number-98,20b)1,G 4.1.04.1.0.3,Sep-17,STUFF,SW 1,Location,GL morestuff,year2020
 
Last edited:
Upvote 0
I get what you're saying, but then I have to find and replace for anywere from 2 to 45, or more, commas...it seems a little unwieldy. I do appreciate the input though!
 
Upvote 0
you can select whole range with text like you show above then do Find and Replace

of course you can use formula or vba but it will be exactly the same with another way :)

good luck
 
Upvote 0
Code:
=SUBSTITUTE(A1, "  ", ",", 1)
inserts a comma in the FIRST instance of a blank space with 2 or more spaces, so i get this:

Code:
txt sometext,                     text and number-98            20b)1            G 4.1.04.1.0.3            Sep-17              STUFF                  SW 1        Location          GL morestuff                        year2020

I just need to figure out how to get it to replace every instance of the 2 or More Blank Spaces in that cell....
 
Upvote 0
Give this macro a try...
Code:
Sub MultipleSpacesToSingleComma()
  Dim V As Variant
  Columns("A").Replace Space(2), ",", xlPart, , , , False, False
  Columns("A").Replace ", ", ","
  For Each V In Array(121, 13, 5, 3, 3, 2)
    Columns("A").Replace String(V, ","), ","
  Next
End Sub
 
Upvote 0
Intriguing macro, Rick. How did you derive the values in the array? Can you show that it works for all sizes of sequential commas (up to some large limit), and even better, if it's an optimal set of numbers?
 
Last edited:
Upvote 0
Rick,

I always appreciate it when you pop in on my threads.

that code worked like a charm.

I would very much like to know what you did there...would you mind commenting the lines so I can use it as an educational opportunity?


I was trying real hard to do this with a plain formula, with various permutations like these:


Code:
=IF(LEN(A1)-LEN(TRIM(A1))>0,SUBSTITUTE(A1, "  ", ",","NO SPACE!"))


 =IF(MID(SUBSTITUTE(A2, "  ", ",", 1))

which, of course, do NOT work.

is this even possible with a plain formula?



Regards,

Blbat
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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