Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace first character in string [closed]

Tags:

excel

vba

I have a string in VBA - Hello how are you.

I want to replace - with something else. How can I do that? I am beginning in VBA. Need help.

like image 404
raju Avatar asked Jan 04 '23 10:01

raju


1 Answers

The easiest way would be to use the replace function.

For example:

 Dim yourString As String
 Dim newString As String
 yourString = "- Hello how are you"
 newString = Replace(yourString, "-", "something else")
 MsgBox newString 'returns "something else Hello how are you"

If it's always the first character and that character is different for each string you can do something like this:

 Dim yourString, subString, replacementString, newString As String
 yourString = "- Hello how are you"
 subString = Right(yourString, Len(yourString) - 1)
 replacementString = "something else"
 newString = replacementString + subString
 MsgBox newString 'returns "something else Hello how are you"
like image 126
Andrew L Avatar answered Feb 13 '23 14:02

Andrew L