MID - Function

MID(string, start [,length])

Returns a substring from the middle, left or right of a string (Variant / String).


stringThe text string (String).
startThe character position to start (Long).
length(Optional) The number of characters to return (Long).

REMARKS
* This function will return a substring from a larger string.
* If "start" > Len("string"), then a zero length string is returned.
* If "start" is 0, then a run-time error occurs.
* The first character position is 1.
* If "string" is Null, then Null is returned.
* If "length" is left blank, then all the characters from the "start" to the end of "string" are returned.
* You can use the INSTR function to return the position of a substring within a larger string, starting at the beginning.
* You can use the INSTRREV function to return the position of a substring within a larger string, starting at the end.
* You can use the LEFT function to return a substring from the left of a string.
* You can use the MID Statement to replace a specified number of characters with characters from another string.
* You can use the MIDB function that can be used with byte data.
* You can use the MID$ function to return a String data type instead of a Variant data type.
* You can use the MIDB$ function to return a String data type instead of a Variant data type.
* You can use the REPLACE function to return a text string with a number of characters replaced.
* You can use the RIGHT function to return a substring from the right of a string.
* For more information, refer to the Finding Strings page.
* The equivalent Excel function is Application.WorksheetFunction.MID
* The equivalent .NET function is Microsoft.VisualBasic.Strings.Mid
* For the Microsoft documentation refer to learn.microsoft.com

Dim sText As String 
sText = Mid("hello world", 4, 5) 'from the middle
Debug.Print sText '= "lo wo"

sText = Mid("hello world", 1, 5) 'from the left
Debug.Print sText '= "hello"

sText = Mid("hello world", 11 + 1 - 5, 5) 'from the right
Debug.Print sText '= "world"

sText = Mid("hello world", 7, 3)
Debug.Print sText '= "wor"

Debug.Print Mid("hello world", 2) '= "ello world"
Debug.Print Mid("abc", 3) '= "c"
Debug.Print Mid("somemore", 5000, 1) '= ""
Debug.Print Mid("somemore", 1, 1) '= "s"
Debug.Print Mid("somemore", 2, 1) '= "so"
Debug.Print Mid("somemore", 5, 4) '= "more"
Debug.Print Mid("development", 0) 'runtime error

Dim sString As String
sString = "123456789"
Debug.Print Mid(sString, Len(sString) - 1) '= "89"
Debug.Print Mid(sString, Len(sString) - 2) '= "789"
Debug.Print Mid(sString, Len(sString) - 3) '= "6789"

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top