检查字符串是否以特定字符结尾并将其删除 - 错误“无效标识符”

Ann*_*nne 2 excel vba

我有一个 VBA 脚本,它应该检查范围内的单元格值是否以“,”结尾。如果是,则删除逗号。

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
Run Code Online (Sandbox Code Playgroud)

然而,在行

 If str.EndsWith(",") Then
Run Code Online (Sandbox Code Playgroud)

我收到错误消息“无效标识符”。但我在我的脚本中找不到错误。非常感谢你的帮助!

Big*_*Ben 7

正如评论中提到的,VBA 与 VB 不同;字符串数据类型在 VBA 中没有属性或方法。

在这种情况下,改变

If str.EndsWith(",") Then
Run Code Online (Sandbox Code Playgroud)

If Right$(str, 1) = "," Then
Run Code Online (Sandbox Code Playgroud)

旁注:考虑使用不同的变量,因为str这会影响Str函数。