Mid, Mid\$ Function

Returns a string that is part of some other string.

Syntax

Mid[\$](stringexpr, start[, length])

Remarks

Mid returns a Variant; Mid\$ returns a String.

The Mid[\$] function has these parts:

Part               Description

stringexpr      String expression from which another string is created.  This can be any string expression.  However, only Mid can accept a Variant of VarType 1 (Null) as stringexpr, in which case, a Null is returned.

start              Long expression that indicates the character position in stringexpr at which the part to be taken begins.

length            Long expression that indicates the number of characters to return.

The arguments start and length must be between 1 and approximately 65,535, inclusive.  If length is omitted or if there are fewer than length characters in the text (including the character at start), the Mid[\$] function returns all characters from the start position to the end of the string.

If start is greater than the number of characters in stringexpr, Mid[\$] returns a zero-length string.

Use the Len function to find the number of characters in stringexpr.

Left, Left\$ Function

Len Function

Mid, Mid\$ Statement

Right, Right\$ Function

Mid, Mid\$ Function Example

The example uses the Mid function to return the middle word from a variable containing three words.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

Sub Form_Click ()

Dim MidWord, Msg, TstStr, SpcPos1, SpcPos2  ' Declare variables.

TstStr = "Mid Function Demo"                ' Create text string.

SpcPos1 = InStr(1, TstStr, " ")             ' Find first space.

SpcPos2 = InStr(SpcPos1 + 1, TstStr, " ")   ' Find next space.

WordLen = (SpcPos2 - SpcPos1) - 1           ' Calculate 2nd word length.

MidWord = Mid(TstStr, SpcPos1 + 1, WordLen) ' Return 2nd word.

Msg = "The word in the middle of Title is """ & MidWord & """"

MsgBox Msg, 0, TstStr                       ' Display message.

End Sub