Written by Allen Wyatt (last updated March 30, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365
If you have used BASIC before, you will be right at home with the three major 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. |
Another function that is handy for dissecting strings is the Split function, which takes just a bit more explanation. This is the syntax for the function:
Split(Source, Delimit, Limit, Compare)
The function returns an array composed of "pieces" of the Source text. Each piece is defined by the presence of the Delimit within the Source. For instance, consider the following macro code:
Dim sPieces() As String Dim sRaw As String sRaw = "abcd-efg-hijklm-nopq" sPieces = Split(sRaw,"-")
When you run the code, the sPieces array will consist of four elements, as follows:
sPieces(0) = "abcd" sPieces(1) = "efg" sPieces(2) = "hijklm" sPieces(3) = "nopq"
The third and fourth parameters for Split are seldom used. Limit indicates the maximum number of array elements you want to return and Compare is used to indicate how you want the comparisons within Source to occur.
Remember that these are all VBA functions, not worksheet functions. Excel provides worksheet functions that do largely the same things, but those functions place different requirements on which parameters are required and which are optional.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12574) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Dissecting a String.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!
Want to use a worksheet function (such as SUM) from within a macro? Here's how easy it is to accomplish the task.
Discover MoreWhen you record a macro, make sure that you know how Excel is recording your cell movements. This tip explains the ...
Discover MoreExcel allows you to hide worksheets so that they aren't visible to those using your workbook. Hiding worksheets has a ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments