Remove comma from the end of string

keith05281967

Board Regular
Joined
May 6, 2011
Messages
68
Greetings VBA wizards,

I'm trying to remove a comma if found at the end of a string. I think i'm really close but I don't have the syntax exactly right. Can you offer some advice?

So an example would be - HOUSTON, to HOUSTON

pretty simple right?

Code:

For i = 2 To lastrow
s = Cells(i, ColumnNumber).Value
s = Trim(s)
If (Right(s, 1)) = "," Then
left(s,len(s)-1),s) <---this is the offending line

End If
Next i


thanks, excel 07. looking for a vba solution
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Code:
If (Right(s, 1)) = "," Then
    s = Left(s, Len(s)-1)
End If

or
Code:
s = Left(s, Len(s) + (Right(s, 1)= ","))
 
Upvote 0
From a syntax perspective I believe you are both right. thank you. The code runs thru the code w/out error but it isn't removing the commas. I ran the code line by line and I can't see why it isn't doing it. I've copied in the code below. Wondering if you see what's missing:


Set Found4 = Cells.Find(What:="City", after:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)

If Found4 Is Nothing Then

MsgBox "City field not found", vbExclamation

Else
Found4.Select
ColumnNumber = ActiveCell.Column
Application.GoTo ActiveCell, Scroll:=True
ActiveCell.ColumnWidth = 30
lastrow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

For i = 2 To lastrow
s = Cells(i, ColumnNumber).Value
s = Trim(s)
If (Right(s, 1)) = "," Then
s = Left(s, Len(s) - 1)

End If
Next i

End If

thanks,
Keith
 
Upvote 0
Try

Rich (BB code):
For i = 2 To lastrow
    s = Cells(i, ColumnNumber).Value
    s = Trim(s)
    If Right(s, 1) = "," Then
        s = Left(s, Len(s) - 1)
        Cells(i, ColumnNumber).Value = s
End If
Next i
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,544
Members
452,925
Latest member
duyvmex

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