tech on the net

MS Excel: MID Function (WS, VBA)

In Microsoft Excel, the MID function extracts a substring from a string (starting at any position).

Syntax

The syntax for the MID function is:

MID( text, start_position, number_of_characters )

text is the string that you wish to extract from.

start_position indicates the position in the string that you will begin extracting from. The first position in the string is 1.

number_of_characters indicates the number of characters that you wish to extract.

Applies To

Type of Function

Worksheet Function Example

Let's take a look at an example to see how you would use the MID function in a worksheet:

Microsoft Excel

Based on the Excel spreadsheet above, the MID function will return the following:

=MID(A1, 5, 4) would return "abet"
=MID(A2, 7, 3) would return "The"
=MID("Excel", 1, 2) would return "Ex"
=MID("Techonthenet.com", 5, 6) would return "onthen"

VBA Function Example

The MID function can also be used in VBA code. For example:

Dim LResult As String

LResult = Mid("Alphabet", 5, 2)

The variable LResult would now contain the value of "ab".