MID - Statement

MID(stringvar, start [,length]) = string

Replaces a specified number of characters with characters from another string.


stringvarThe text string (String).
startThe character position to start (Long).
length(Optional) The number of characters to return (Long).
stringThe string expression that replaces part of stringvar (String).

REMARKS
* You cannot prefix this statement with "VBA."
* This statement will replace a specified number of characters in a string variable.
* VBA.Mid always calls the MID Function which means you will probably see a Run-time error 424 Object required.
* The number of characters replaced is always less than or equal to the number of characters in stringvar.
* The first character position is 1.
* You can use the LEFT function to return a substring from the left of a string.
* You can use the MID Function to return a substring from the middle, left or right of a string.
* You can use the RIGHT function to return a substring from the right of a string.
* For more information, refer to the Replacing Strings page.
* For the Microsoft documentation refer to learn.microsoft.com

Dim sText As String 
sText = "hello world"
Mid(sText, 1, 3) = "XXX"
Debug.Print sText '= "XXXlo world"

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

sText = "The dog jumps"
Mid(sText, 5, 3) = "fox"
Debug.Print sText '= "The fox jumps"

Mid(sText, 5) = "cow"
Debug.Print sText '= "The cow jumps"

Mid(sText, 5) = "cat runs fast"
Debug.Print sText '= "The cat runs"

Mid(sText, 5, 3) = "rabbit"
Debug.Print sText '= "The rab runs"

VBA.Mid(sText, 5, 3) = "rabbit" 'Runtime error 424 Object required

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