VBA Trick of the Week :: MID() Function

Posted: May 30, 2013 by MaxFraudy in VBA
Tags: , ,

MID() Function is generally used to extract a substring from a string. The syntax of the function is:

                          MID(string, Start, [Length])

Here, string is the string that is being manipulated, Start is the position from which substring is to be extracted and Length is the number of characters that are to be extracted. We usually see its use on the right hand side of the “=” sign as below:

                        strSubString = Mid(“Orange Pie”,1,6)

which assigns “Orange” to the variable strSubString. Suppose now I wish to substitute the substring “Orange” with “Banana”. Perhaps the most common way of doing it is by using Replace function. However, this can also be done using the MID function (this time on the left hand side of the “=” sign) as below:

                       strString = Replace(strString,”Orange”,”Banana”)

                       Mid(strString, 1, 6) = “Banana”



  1. snb says:

    In this case you are not using the [b]function[/b] Mid, but the [b]Statement[/b] MId.
    That is quite another thing although their names do not differ.
    It only works replacing a string in a varialbe that contains a string.
    Mid(“illustriation”,3,4)=”enab” gives an syntax error.

Share your thoughts/feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s