2016-11-14 4 views
2

Ich habe versucht mit unten Code, aber nicht in der Lage, Komma zu entfernen. Bitte helfen Sie.Wie wird das letzte Komma in einer gegebenen Zeichenfolge entfernt?

Sub removelastcommas() 

    Dim i As Integer, str As String 

    str = Range("A1") 

    For i = Len(str) To 1 
     If Mid(str, i, 1) <> "," Then 
      Exit For 
     End If 
    Next 
    Range("b1") = Left(str, i) 

End Sub 
+2

' = SUBSTITUTE (A1, ",", "", LEN (A1) -LEN (ERSATZ (A1, ",", ""))) –

Antwort

2

Eine weitere Option, mit InStrRev Funktion (ohne Verwendung einer Schleife) ist:

Sub removelastcommas() 

    Dim i As Integer, str As String 
    str = Range("A1") 

    i = InStrRev(str, ",") 
    ' comma found in A1 
    If i > 0 Then 
     Range("B1") = Left(str, i - 1) & Right(str, Len(str) - i) 
    Else ' comma not found in A1 
     Range("B1") = Range("A1") 
    End If 

End Sub 
2

Dies wird das letzte Komma aus einem String entfernen:

Sub removelastcommas() 
    Dim i As Integer, str As String 
    str = Range("A1") 
     For i = Len(str) To 1 Step -1 
      If Mid(str, i, 1) = "," Then 
       Range("B1").Value = Left(str, i - 1) & Mid(str, i + 1) 
       Exit Sub 
      End If 
     Next i 
End Sub 

enter image description here

Verwandte Themen