Skip to main content

Mid statement

Table of contents
  1. Syntax
  2. Remarks
  3. Example

Replaces a specified number of characters in a Variant (String) variable with characters from another string.

Syntax

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

The Mid statement syntax has these parts:

Part Description
stringvar Required. Name of string variable to modify.
start Required; Variant (Long). Character position in stringvar where the replacement of text begins.
length Optional; Variant (Long). Number of characters to replace. If omitted, all of string is used.
string Required. String expression that replaces part of stringvar.

Remarks

The number of characters replaced is always less than or equal to the number of characters in stringvar.

Example

This example uses the Mid statement to replace a specified number of characters in a string variable with characters from another string.

Dim MyString 
MyString = "The dog jumps" ' Initialize string. 
Mid(MyString, 5, 3) = "fox" ' MyString = "The fox jumps". 
Mid(MyString, 5) = "cow" ' MyString = "The cow jumps". 
Mid(MyString, 5) = "cow jumped over" ' MyString = "The cow jumpe". 
Mid(MyString, 5, 3) = "duck" ' MyString = "The duc jumpe".

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>