What is VBA MID Function
The VBA MID function is listed under the text category of VBA functions. When you use it int a VBA code, it can get a sub-string from a string using the starting position and the number of characters to extract. In simple words, you extract specific characters from a string using a starting position and by defining the number of characters to extract after that.
how to use it
To use VBA's MID function you need to understand its syntax and arguments:
Mid(Str, Start, [Length])
- Str: The string from which you want to extract a sub-string.
- Start: The starting position of the sub-string in the string.
- [Length]: The length of the sub-string (This is an optional argument and if omitted VBA extract all the characters starting from the Start to end of the string by default).
Example to use MID Function in VBA
To practically understand how to use VBA MID function, you need to go through the below example where we have written a vba code by using it:
Sub example_MID() Range("B1").Value = Mid(Range("A1"), 7, 6) End Sub
In the above code, we have used MID to get the string from the value which we have in the cell A1 and in the function, we have mentioned the starting point 7 and total 6 number of characters to get.
Below are some important points which you need to take care while using MID function in VBA.
- If the supplied string is NULL, then it will return NULL in the result.
About the Author
Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can ﬁnd him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.