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(ActiveSheet.Range(ActiveWindow.Selection.Address))
When this line is executed, it returns the length of whatever is in the currently selected cell.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12404) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Determining the Length of a String.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Macros often need to select different cells in a worksheet. Here's how you can use macro commands to change which cell is ...
Discover MoreIf you run a macro you used in an older version of Excel on a newer system, it may seem like the macro runs slower. Here ...
Discover MoreWant a quick way to jump to the end of your data entry area in a worksheet? The macro in this tip makes quick work of the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2017-01-09 22:43:17
Greg
The statement as shown in the tip is a fully qualified cell location within a workbook..specifically the actual currently selected cell.
You could use a similar statement to reference a cell on another sheet in the same workbook, or even a cell in another workbook so long as the file path was a constant...but I don't have the syntax handy to share.( ie, an external reference)
2017-01-07 19:13:50
Bob Beechey
To reveal the number of characters in active cell (string or not) you could use:
C = Len(ActiveCell.Text)
2017-01-07 17:51:18
John M
When I read the title of the tip, I didn't realize it was a VBA. For non VBA use, the LEN function works much the same way. For example "=LEN(D1)" (without the quotes) will return the length of the string in cell D1. This works with cells formatted as text or general.
2017-01-07 06:28:35
Alex B
Is the full specification,
Len(ActiveSheet.Range(ActiveWindow.Selection.Address)),
an Excel 2016 requirement ?
In Excel 2010 with selection being A2 both,
c = len(activecell)
c = len(cells(2,1))
seem to work fine for me.
I hear that Excel 2016 gets a bit tricker with the allowing of multiple windows.
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 © 2021 Sharon Parq Associates, Inc.
Comments