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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
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,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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