String Manipulation
Join Strings | Left | Right | Len | Instr | Mid
There are many functions in Excel VBA we can use to manipulate strings. Below you can find a review of the most important functions.
Place a command button on your worksheet and add the code lines described in this chapter. To execute the code lines, click the command button on the sheet.
Join Strings
We use the & operator to concatenate (join) strings.
Code:
text1 = "Hi "
text2 = "Tim"
MsgBox text1 & text2
Result:
Left
To extracts the leftmost characters from a string, use Left.
Code:
text = "example text"
MsgBox Left(text, 4)
Result:
Right
To extracts the rightmost characters from a string, use Right. We can directly insert text in a function as well.
Code:
Result:
Len
To get the length of a string, use Len.
Code:
Result:
Note: space (position 8) included!
Instr
To find the position of a substring in a string, use Instr.
Code:
Result:
Note: string "am" found at position 3.
Mid
To extract a substring, starting in the middle of a string, use Mid.
Code:
Result:
Note: started at position 9 (t) with length 4.
Did you find this information helpful? Show your appreciation, vote for us.
Go to Top: String Manipulation | Go to Next Topic: Calculate