VBA String Functions

Publication Date :

Blog Author :

Table Of Contents

arrow

Excel VBA String Functions

VBA String functions do not replace the string, but the result of these functions creates a new string. There are many string functions in VBA. They are all categorized under String or Text functions. Some important functions are the LEFT function to get the value from the left and the RIGHT function to get the value from the right or the MID function, LEN, and INSTR function.

String functions are so important. We can extract any characters from the string by finding the number of characters of the supplied string.

We can extract characters from the left side of the string, we can extract from the right side of the string, we can extract from the middle of the string, we can combine two texts, and we can split them at the same time as well.

It is important to have a fair bit of knowledge about all these VBA functions as part of the large project.

List of Top 6 String Functions in VBA

  1. LEN Function
  2. LEFT Function
  3. RIGHT Function
  4. MID Function
  5. TRIM Function
  6. Instr Function
VBA String Functions

One thing we would like to say is that "the VBA string functions are text functions in the worksheet."

You must have already used LEN, LEFT, RIGHT, MID, and SUBSTITUTE excel functions to play around with the data. In VBA, too, we can use them to play around with the data.

We will discuss some of the important functions of this article.

#1 - LEN Function

LEN stands for "LENGTH." Therefore, it will give us the number of characters involved in the supplied string. For example, if you supply the word "Hello," LEN in the excel function will return 5 because there are five characters in the word "Hello."

The below code will show the example.

Code:

Sub LEN_Example()

  Dim TotalCount As String

  TotalCount = Len("Hello")

  MsgBox TotalCount

End Sub

It will show the result in the message box as 5.

VBA LEN Example

#2 - LEFT Function

We need to use the VBA LEFT function to extract the characters from the left side of the string. First, take a look at the syntax of the LEFT function.

LEFT Formula
  • The string is what the string we are trying to extract is.
  • Length is nothing but how many characters you need from the left side of the supplied String.

Code:

Sub LEFT_Example()

  Dim FirstName As String

  FirstName = Left("Sachin Tendulkar", 6)

  MsgBox FirstName

End Sub

It will extract the first six characters from the string "Sachin Tendulkar." So, the result will be the first name,  "Sachin."

VBA LEFT Example

#3 - RIGHT Function

Like how we have extracted values from the left side of the string similarly, we can also extract from the right side of the string.

The syntax of the RIGHT function is the same as the LEFT function.

Right Formula
  • The string is what the string we are trying to extract is.
  • Length is nothing but how many characters you need from the right side of the supplied String.

Code:

Sub RIGHT_Example()

  Dim LastName As String

  LastName = Right("Sachin Tendulkar", 9)

  MsgBox LastName

End Sub

It will extract 9 characters from the string "Sachin Tendulkar." So, the result will be the last name "Tendulkar."

VBA Right Example

#4 - MID Function

We can extract the characters from the left and right sides and the middle of the string. Below is the syntax of the VBA MID function.

MID formula
  • String to Search: From which string do we need the middle value?
  • Starting Position: What is the starting character position number to extract?
  • Number of Characters to Extract: How many characters need to be extracted from the Starting Position?

For example, if the name is "Sachin Ramesh Tendulkar," the middle name is "Ramesh" in this string starting position of the character to be extracted 8. Therefore, we need six characters from the starting position. The below code will extract the middle value.

Code:

Sub MID_Example()

  Dim MiddleName As String

  MiddleName = Mid("Sachin Ramesh Tendulkar", 8, 6)

  MsgBox MiddleName

End Sub

It will extract "Ramesh" from the middle of the string "Sachin Ramesh Tendulkar."

VBA String Functions Example

#5 - TRIM Function

TRIM is the function of cleaning the data. It will eliminate unwanted space characters from the string. First, take a look at the syntax of the TRIM function.

Trim formula

It is straightforward what value or string you want to trim.

For example, assume you have the string "Hello, How are you?." Here, we have unnecessary space characters before the word "Hello," so by using TRIM. We can eliminate this.

Code:

Sub TRIM_Example()

  Dim MyValue As String

  MyValue = Trim(" Hello How are you?")

  MsgBox MyValue

End Sub

It will delete the unwanted space characters from the supplied string.

VBA String Functions Example

Note: VBA TRIM is slightly different from the worksheet function. For a detailed explanation of this VBA function, refer to our article on "VBA TRIM."

We have LTRIM and RTRIM functions as well in VBA. For example, LTRIM will delete unwanted spaces from the left side of the string, and RTRIM deletes unwanted spaces from the right side.

#6 - Instr Function

The Instr function helps find the position of the supplied character in the string. The syntax of the INSTR function is as follows:

Instr Formula
  • From which position of the supplied string do we need the position?
  • What is the string you are referring to?
  • What character are you searching for in ?

For example, if you have the word "Recipe" and you want to find the position of the character "e" from the first place below, the code will show the position of the letter "e."

Code:

Sub INSTR_Example()

  Dim MyValue As String

  MyValue = InStr(1, "Recipe", "e")

  MsgBox MyValue

End Sub

So, from the first position of the string letter "e" position is 2.

VBA String Functions Example

If you want the position of the second appearance of the letter "e," then you need to use the Start argument as 3.

Code:

Sub INSTR_Example()

  Dim MyValue As String

  MyValue = InStr(3, "Recipe", "e")

  MsgBox MyValue

End Sub

So, in this case, the position of the letter "e" after the first appearance is 6th.

VBA String Functions Example 1

These are some of the important String functions. We hope you have enjoyed it.