Check if a string ends with a specific character and remove it - error "invalid identifier"

Viewed 3642

I have a VBA script which should check if the cell values in a range end with ",". If so, the comma is removed.

Dim Bereich As Range
Dim Zeilen As Long
Dim Zelle As Range
Dim str As String

Zeilen = Cells(Rows.Count, 1).End(xlUp).Row
Set Bereich = Range(Cells(4, 3), Cells(Zeilen, 19))

For Each Zelle In Bereich

str = Zelle.Value

If str.EndsWith(",") Then
str = Left(str, Len(str) - 1)
Zelle.Value = str
End If

Next Zelle

However, in the line

 If str.EndsWith(",") Then

I get the errror-message "invalid identifier". But I can't find the fault in my script. Thank you so much for your help!

1 Answers

As mentioned in the comments, VBA is different from VB; the string data type does not have properties or methods in VBA.

In this case, change

If str.EndsWith(",") Then

to

If Right$(str, 1) = "," Then
Related