How to delete last occurence of a character in vba

spyrule

Board Regular
Joined
Aug 21, 2015
Messages
114
Hello,
I've got a weird problem where I'm struggling to detect the last occurence of the character "-" and replace it with "x".

Does anybody have a way to take a string and replace only the last occurence of that character? (I've gotten a replace function to work, but it replaces ALL occurences, which isn't what i wanted).

Any helps is appreciated!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi,
Let's assume you have a text in cells(1,1) you want to replace last occurence of "-" with "x". You have to loop backwards the string from last char to first and check the first occurence of "-". Then you have to use this char position in replace function as a 4th parameter.

Check below code.

Code:
Sub CharReplacement()
Dim str_text$
Dim i&

str_text= cstr(cells(1,1))
If len(str_text)>0 then
   For i =len(str_text) to 1 step -1
      If right(left(str_text,i),1)="-" then
         cells(1,1)=Replace (str_text, "-","x",i)
         exit for
      End if
   Next i
End if

End sub
 
Upvote 0
Here's a UDF you can try:
Code:
Function ReplaceLastOne(S As String) As String
Const Char As String = "-"
Dim Position As Long
If InStr(S, Char) = 0 Then
    ReplaceLastOne = S
    Exit Function
End If
Position = InStrRev(S, Char)
ReplaceLastOne = Mid(S, 1, Position - 1) & Replace(S, Char, "x", Position, 1)
End Function
 
Upvote 0
Ugly way that appears to work (based on string being in A1, result in A2 just for testing)

Code:
Sub replaceit()
Cells(2, 1) = StrReverse(Replace(StrReverse(Cells(1, 1).Value), "-", "x", , 1))
End Sub


I don't think it will be as efficient as what Eric W posted in post number 2 but I haven't tested them for speed.
 
Upvote 0
Code:
Function ReplaceLastChar(ByVal S As String, FindChar As String, ReplaceChar As String) As String
    Dim SPos As Long

    SPos = InStrRev(S, FindChar)
    If SPos > 0 Then
        S = Left(S, SPos - 1) & ReplaceChar & Mid(S, SPos + 1, Len(S))
    End If
    ReplaceLastChar = S
End Function
 
Upvote 0
Another way. Takes 3 arguments. 1.) The value 2.) The character to replace, e.g. "-" 3.) The character to replace it with, e.g. "x".

Code:
Function REPLACELAST(s As String, findStr As String, repSTR) As String
With CreateObject("VBScript.RegExp")
    .Pattern = "(" & findStr & ")([^" & findStr & "]+$)"
    REPLACELAST = .Replace(s, repSTR & "$2")
End With
End Function
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

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