replace last character in string

M

Mike H

Maybe this

myString = Range("A1").Value
NewEnd = "W"
myString = Left(myString, Len(myString) - 1) & NewEnd
Range("A1").Value=myString

Mike
 
R

Rick Rothstein

This is one way...

StrVariable = Left(StrVariable, Len(StrVariable) - 1) & YourNewCharacter
 
R

RonaldoOneNil

To replace last character in string called strTemp with a "G" use this

strTemp = Left(strTemp, Len(strTemp) - 1) & "G"
 
R

Rick Rothstein

If your text is in a simple variable, then you can also do it this way...

Mid(StrVariable, Len(StrVariable), 1) = YourNewCharacter

This is more efficient than using the concatenation method I posted earlier,
but it requires your text to be in a simple variable (not a Range or other
text holding object) in order to work.
 
R

Rick Rothstein

And just to "have fun" (in other words, do not actually use what
follows<g>), here is a method that works as long as your text is in not a
Unicode font (that is, it only uses ASCII/ANSI character codes of 32 to
255)...

Temp = Split(StrConv(StrVariable, vbUnicode), Chr(0))
Temp(UBound(Temp) - 1) = YourNewCharacter
StrVariable = StrConv(Join(Temp, Chr(0)), vbFromUnicode)

where you would Dim the Temp variable as either a Variant or a dynamic
String array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top