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

Making Common Information Accessible

Got a bunch of info that is common to a lot of your documents? Here's a way to get that information standardized among all ...

Discover More

Saving AutoText Entries with Each Document

AutoText can be a great way to add consistent, common text to a document. Unfortunately you cannot save AutoText entries with ...

Discover More

Adjusting Column Width from the Keyboard

It's easy to adjust the width of table columns using the mouse, but what if you don't want to use the mouse? Adjusting column ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Understanding the While...Wend Structure

Logical structures are important in programming, as they allow you to control how the programming statements are executed. ...

Discover More

Running Macros in the Background

Want to run a macro in Excel, but not sure if doing so will tie up your computer? Here's how macro processing really happens.

Discover More

Determining the Hour of the Day

Need to know the current hour of the day? You can derive the information in your macros by using the Hour function, as ...

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 8Mpixels. 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 three more than 5?

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.