How to use the VBA MID Function (Syntax + Example)

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

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.

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 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

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

Related Functions

FORMAT | INSTR | INSTRREV | LCASE | LEFT | LEN | LTRIM | REPLACE | RIGHT | RTRIM | SPACE | STRCOMP | STRCONV | STRING | STRREVERSE | TRIM | UCASE