VBA MID Function

HomeVBA Functions LIST (Category Wise)How to use the VBA MID Function (Syntax + Example)

What is VBA MID Function

The VBA MID function is listed under the text category of VBA functions. When you use it int a VBA code, it can get a sub-string from a string using the starting position and the number of characters to extract. In simple words, you extract specific characters from a string using a starting position and by defining the number of characters to extract after that.

how to use it

To use VBA's MID function you need to understand its syntax and arguments:

Syntax

Mid(Str, Start, [Length])

Arguments

  • Str: The string from which you want to extract a sub-string.
  • Start: The starting position of the sub-string in the string.
  • [Length]: The length of the sub-string (This is an optional argument and if omitted VBA extract all the characters starting from the Start to end of the string by default).

Example to use MID Function in VBA

To practically understand how to use VBA MID function, you need to go through the below example where we have written a vba code by using it:

Sub example_MID()
Range("B1").Value = Mid(Range("A1"), 7, 6)
End Sub

In the above code, we have used MID to get the string from the value which we have in the cell A1 and in the function, we have mentioned the starting point 7 and total 6 number of characters to get.

Notes

Below are some important points which you need to take care while using MID function in VBA.

  • If the supplied string is NULL, then it will return NULL in the result.

Related Functions

About the Author

puneet one point one

Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can find him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.