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).

* This statement will replace a specified number of characters in a string variable.
* This statement cannot have a VBA. prefix.
* 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 MID Function to return the text string which is a substring of a larger 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

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