Remove Trailing Semicolons

dfolzenlogen

New Member
Joined
Oct 18, 2009
Messages
36
I found the following formula for removing trailing commas on this site:

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


I want to remove trailing semicolons so I changed the formula to read as follows:

Sub RemoveTrailingSemicolon()
Dim Cell As Range
For Each Cell In Selection
Cell.Value = Replace(Replace(RTrim(Replace(Replace(Cell.Value, " ", Chr(59)), ";", " ")), " ", ";"), Chr(59), " ")
Next
End Sub

Unfortunately, the above formula is removing all other semicolons in the cell which is separating other data. What am I missing?

Thanks.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Leave Chr(1) as it was.
 
Last edited:
Upvote 0
.. and other options would be
Code:
Sub RemoveTrailingSemicolon_v2()
  Dim Cell As Range
  
  With CreateObject("VBScript.RegExp")
    .Pattern = ";+$"
    For Each Cell In Selection
      Cell.Value = .Replace(Cell.Value, "")
    Next Cell
  End With
End Sub

Sub RemoveTrailingSemicolon_v3()
  Dim Cell As Range
  Dim i As Long
  
  For Each Cell In Selection
    i = Len(Cell.Value) + 1
    Do While Mid("x" & Cell.Value, i, 1) = ";"
      i = i - 1
    Loop
    Cell.Value = Left(Cell.Value, i - 1)
  Next Cell
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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