
Tips.Net > ExcelTips Home > Macros > VBA Examples > Determining the Length of a String
Summary: In a macro it is often helpful to know how long a particular text string is. That's where the Len() function proves invaluable, as described in this tip. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
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.
Tip #2299 applies to Microsoft Excel versions: 97 2000 2002 2003
PivotTables Got You Perplexed? ExcelTips: PivotTables for the Faint of Heart shows how you can start using Excel's PivotTable tool right away to spin your data into gold! You discover how easy it really is to crunch the numbers you need to crunch. Uncover the power of the PivotTable Wizard, how to edit PivotTables, how to format them, how to customize them, and much more.
Check out ExcelTips: PivotTables for the Faint of Heart today!
Add power to your purpose with Excel. A comprehensive 500+ page e-book explains everything you need to know about macros. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site