Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Dissecting a String.

Dissecting a String

by Allen Wyatt
(last updated April 18, 2017)

5

If you have used BASIC before, you will be right at home with the string functions provided by VBA. The following table details the most common string functions and what they return.

Function Comments
Left(Source, Count) Returns the left Count characters of Source text.
Mid(Source, Start [, Count]) Returns the portion of Source text beginning with the Start character. If Count is supplied, then the result is limited to that many characters.
Right(Source, Count) Returns the right Count characters of Source text.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12574) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Dissecting a String.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Selecting a Word

Selecting text is a critical skill to possess when you want to work with a Word document. This tip explains how you can ...

Discover More

Removing Duplicate Cells

If you need to often delete duplicate items from a list, then you'll love the macro presented in this tip. It makes quick ...

Discover More

Counting Commas in a Selection

If you have a range of cells in which you want to count all the commas, there are several ways you can derive the figure you ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Understanding Variables in VBA Macros

You can create and use all sorts of variables in your macros. This tip examines all the different data types you can specify.

Discover More

Telling which Worksheets are Selected

If your macro processes information on a number of worksheets, chances are good that you need your macro to figure out which ...

Discover More

Converting Strings to Numbers

When working with data in a macro, there are two broad categories you can manipulate: numbers and text. Sometimes you need to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 4?

2017-04-20 10:36:22

Peg Molter

Thanks so much for the clarification, guys. It all makes sense now!


2017-04-20 04:39:27

Alan Elston

Hi Willy, Hi Micky
Hi Peg Molter,
As Willy said we are talking in this Blog about the VBA Strings collection function MID.
The Excel VBA documentation seems to be getting increasingly bad and I often mix Excel Spreadsheet Functions and VBA Functions up.
For VBA Strings Functions you are best looking at some old VB documentation, such as
vb6.us/tutorials/vb6-string-functions
You will also there find the gem of info that you can use Mid on the left side of an assignment statement, where you can replace a substring within a string.
In VBA the differences are not a great problem, as you have the Evaluate Method which allows you to do most spreadsheet things within VBA.
Doing it the other way around is not too difficult either. Even if you are not familiar with VBA, I would strongly recommend learning the very basics so that you could, for example, make your own MID function. Then you could arrange that you had a MIDPEG function, that will work for you in the spreadsheet just as VBA Strings collection MID works.
So this would be your VBA code to make your MIDPEG function. You put that in a Normal Code Module.

Public Function MIDPEG(ByVal Str As String, ByVal Stt As Long, Optional Lenf As Long) As String ' https://excelribbon.tips.net/T012574_Dissecting_a_String.html
If Lenf = 0 Then
Let MIDPEG = Mid(Str, Stt)
Else
Let MIDPEG = Mid(Str, Stt, Lenf)
End If
End Function

After that, if you type =MIDPEG in any cell you will be able to use it just like the MID function in VBA, that is to say you can omit the last Count argument.
MIDPEG.jpg imgur.com/0ZcuTaI
Try it in this File, ( you will need to enable macros , if prompted to do it on opening ).
app.box.com/s/fueek0ccuc7715d1v2x5qqqxkvyd5jqs
If you are not too familiar with macros then check out some of Allen’s info on macros

Alan


2017-04-20 03:16:43

Michael (Micky) Avidan

@Peg Molter,
In addition to Willy Vanhaelen's explanation - I would like to emphazise that (in VBA) if the Num_chars argument is omitted or if there are fewer than Length characters in the text (including the character at position Start), all characters from the Start position to the End of the string - are returned.
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2017)
ISRAEL


2017-04-19 12:15:36

Willy Vanhaelen

@Peg Molter
The Count argument is not optional in the Excel MID function but, as mentioned in the first sentence, this tip is about VBA (Visual Basic for Applications, used in Excel macros) and there it is optional.
SomeVariable = Mid("VBA Mid function",5,3) returns "Mid"
SomeVariable = Mid("VBA Mid function",5) returns "Mid function"


2017-04-18 07:44:50

Peg Molter

The argument Count is not optional for the MID function. I tried it without it, wondering what it would return. It returned an ERROR and told me I had too few arguments.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.