VBA MID Function (Syntax + Example)

- Written by Puneet

The VBA MID function is listed under the text category of VBA functions. When you use it in 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.

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 the end of the string by default).

Example

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:

example-vba-mid-function
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 cell A1 and in the function, we have mentioned the starting point 7 and a total 6 number of characters to get.

Notes

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