Get rid of ALL trailing commas in a text string

diannao man

New Member
Joined
Dec 19, 2011
Messages
7
Hi all

I'm using Excel 2003, Windows XP

I'm trying to get rid of all trailing commas in a text string, for example:

"Steve Smith,,,,,,,,,,,"
"Sarah Golding,,,,,,"

The number of trailing commas varies in a list of 500 names - I've tried a few formulas including these two:

=LEFT(A1,LEN(A1)-1)&SUBSTITUTE(RIGHT(A1), ",", "")
=IF(RIGHT(TRIM(A1),1)=",",LEFT(TRIM(A1),LEN(TRIM(A1))-1),A1)

But these only delete the last comma, where as I'm trying to delete all trailing commas. I've searched these boards somewhat, but can only find formulas similar to the ones above. Can anyone help?

Thanks very much

Diannao man
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi Steve, thanks for your quick reply. Unfortunately I can't use find/replace as there are other commas which need to be preserved - most of the names are as the examples I posted, but there are quite a few like:

Karen Jones, Jim Bob,,,,,,,,,,,

I only need to get rid of the trailing commas in cases like that

thanks again
 
Upvote 0
Hi Gerald, basically only the commas at the end of the text string so in:

Karen Jones, Jim Bob, Sarah Golding,,,,,,,,,,

The commas after "Golding" all need to be deleted, whereas the others need to be kept in - is that even possible!!?
 
Upvote 0
For a one time routine, you could put this in B1 (dragged down)
=IF(RIGHT(A1,1)=",",LEFT(A1,LEN(A1)-1),A1)

and drag that right until they are all gone.

For regular use, you could use this CSE formula

=REPLACE(A1,MATCH(1, IF(MID(A1&REPT(",",26), COLUMN(A1:Z1), 1)<>",",1))+1, 255, "")

entered with Ctl-Shift-Enter (Cmd+Return for Mac)
 
Upvote 0
Replace 2 commas with one comma. Keep repeating "Replace All" until all strings have a single trailing comma and then use your original formula.

Gary
 
Upvote 0
If you are trying to "purify" your actual data, in place, the you will need a VBA macro to do it. Select the cells containing your data (it is alright if cells without trailing commas are selected as they will not be changed) and then run either one of these macros... your choice as they both do the same thing, but use different techniques to accomplish their goal...

Code:
Sub RemoveTrailingCommas_Version1()
  Dim Cell As Range
  For Each Cell In Selection
    Cell.Value = Replace(Replace(RTrim(Replace(Replace(Cell.Value, " ", Chr(1)), ",", " ")), " ", ","), Chr(1), " ")
  Next
End Sub
 
Sub RemoveTrailingCommas_Version2()
  Dim X As Long, Cell As Range, CellVal As String
  For Each Cell In Selection
    CellVal = Cell.Value
    For X = Len(CellVal) To 1 Step -1
      If Mid(CellVal, X, 1) <> "," Then
        Cell.Value = Left(CellVal, X)
        Exit For
      End If
    Next
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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