VBA Function: Mid

The VBA Mid function returns a specified number of characters from a string starting from the defined character position.

Usage:

Mid(text, start_character)

or

Mid(text, start_character, num_chars)


Example of Usage

Using the Mid function to extract different portions of text from the string:

Sub example()
    
    MsgBox Mid("www.excel-pratique.com", 1, 1) 'Returns: w
    MsgBox Mid("www.excel-pratique.com", 1, 3) 'Returns: www
    MsgBox Mid("www.excel-pratique.com", 1, 9) 'Returns: www.excel
    MsgBox Mid("www.excel-pratique.com", 1, 30) 'Returns: www.excel-pratique.com
    
    MsgBox Mid("www.excel-pratique.com", 5, 5) 'Returns: excel
    MsgBox Mid("www.excel-pratique.com", 5, 14) 'Returns: excel-pratique
    MsgBox Mid("www.excel-pratique.com", 11, 8) 'Returns: pratique
    MsgBox Mid("www.excel-pratique.com", 20, 3) 'Returns: com
    MsgBox Mid("www.excel-pratique.com", 5, 50) 'Returns: excel-pratique.com
    MsgBox Mid("www.excel-pratique.com", 5) 'Returns: excel-pratique.com

End Sub