VBA Mid function

Mid Function Description

The VBA Mid function extracts a substring from a given string, based on a given position within the string and a number of characters to extract.

If you want to get a substring starting from the left use Left and if you want to get a substring starting from the right (the end of the string) use Right.

Syntax

The syntax for the Mid function in VBA is:

MID( text, start_position, no_of_characters )

Parameters

text
The string from which you wish to extract from.

start_position
The index position in the string that the function is to begin extracting from. The first index position in the string is 1.

no_of_characters
The number of characters that you wish to extract.

Other Notes

The string index position in the Mid, Left and Right functions starts at 1.

Example usage

The Mid function can be used in VBA code. Let’s look at some VBA Mid function examples:

Mid "Hello World!", 6, 6
'Result: "World"

Mid "Hello World!", 1, 5
'Result: "Hello"