Mid Statement
Replaces a specified number of characters in a Variant (String) variable with characters from another string.
Syntax
Mid(stringvar, start[, length]) = string
stringvar: Required. Name of string variable to modifystart: Required. Character position where replacement begins (1-based)length: Optional. Number of characters to replace. If omitted, uses entire length ofstringstring: Required. String expression used as replacement
Remarks
- The number of characters replaced is always less than or equal to the number of characters in
stringvar - If
startis greater than the length ofstringvar,stringvaris unchanged - If
lengthis omitted, all characters fromstartto the end of the string are replaced Midstatement replaces characters in-place; it does not change the length of the original string- If replacement string is longer than
length, onlylengthcharacters are used - If replacement string is shorter than
length, only available characters are replaced
Examples
Dim s As String
s = "Hello World"
Mid(s, 7, 5) = "VB6!!" ' s becomes "Hello VB6!!"
s = "ABCDEFGH"
Mid(s, 3) = "123" ' s becomes "AB123FGH"
s = "Test"
Mid(s, 2, 2) = "XX" ' s becomes "TXXt"