Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. 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: Determining the Length of a String.
Written by Allen Wyatt (last updated December 17, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
It is hard to imagine a function used more often with strings than the Len function. This simple little function returns the length of any string. The following are a few examples:
A = Len(MyString) B = Len("This is a test")
The first line returns the length of the characters in the variable MyString. The second returns the number of characters between the quote marks (in this case, 14—remember that spaces count as characters).
If you want to determine the length of the information in a particular cell, you follow a bit different approach:
C = Len(ActiveCell.Text)
When this line is executed, it returns the length of whatever is displayed in the currently selected cell. It is important to note that since it relies on the .Text property, the code returns the length of what is displayed in the cell, not what is actually contained within the cell.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12404) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Determining the Length of a String.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Macros are great when it comes to automating how you work with your workbooks. What if you want to fundamentally change ...
Discover MoreExcel allows you to specify the RGB (red, green, and blue) value for any color used in a cell. Here's a quick way to see ...
Discover MoreDoes your macro need to change the width of some columns in a worksheet? Here's how to do it.
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 © 2024 Sharon Parq Associates, Inc.
Comments