VBA – Mid String
The Mid
function in VBA extracts a specific portion (substring) from a given string.
Syntax:
Mid(string, start, [length])
Parameter:
Return Value:
Extract a specific number of characters
Dim str1 As String
Dim subStr As String
str1 = "Hello, World!"
subStr = Mid(str1, 7, 5)
' subStr will be "World"
Extract characters from a specific position to the end
Dim str1 As String
Dim subStr As String
str1 = "Hello, World!"
subStr = Mid(str1, 7)
' subStr will be "World!"
Use Cases:
Key Considerations:
start
is less than 1, an error occurs.start
is greater than the length of the string, an empty string is returned.length
is omitted, the function extracts all characters from the start
position to the end of the string.