The Excel LEN function, also known as LENGTH function, is a Text formula that returns the number of characters in a given string. In this guide, we’re going to show you how to use the Excel LEN function and also go over some tips and error handling methods.

 

Supported versions

The LEN function is its compatibility across all versions of Excel. This universal applicability ensures that users working on different versions of Excel can seamlessly utilize this function without any version-specific limitations or modifications.

 

Excel LEN Function Syntax

The syntax of the Excel LEN function is straightforward and user-friendly with only one argument:

LEN(text)

text: This parameter represents the string whose length you are interested in calculating. It can be a direct text input or a reference to a cell containing the text.

 


Examples

To better understand the application of the LEN function, let's delve into some examples:

Basic Use Case of LEN Function

The primary function of the LEN command is to compute the length of a given string. This is achieved by simply providing the text or a reference to the text. For example,

=LEN("2 + 2 equals 5")

This formula would return the value 14. This result is derived from counting every character in the string, including letters, numbers, symbols, and even spaces.

Basic use of LEN function to count characters in Excel

Using LEN Formula to Handle Formatted Strings

An important aspect to note is that the LEN function does not consider the number formatting in cells. Therefore, irrespective of how a number is formatted in a cell, the LENGTH function will return the length of the unformatted number.

Use of LEN function to count formatted characters in Excel

 

 Download Workbook

 

What is the Difference Between LEN and LENB Functions

The LEN and LENB functions in Excel are both designed for counting characters in a text string, but they differ significantly in their approach and use cases, especially in relation to character encoding. The LEN function is the more commonly used of the two. It counts the number of characters in a given text string, treating each character as a single entity regardless of its encoding as explained in detail in this article. On the other hand, the LENB function, where 'B' stands for 'byte', is tailored for a more specific purpose. It counts the number of bytes used to represent the characters in a text string. This is particularly relevant for languages that use a double-byte character set (DBCS), such as Japanese, Chinese, or Korean. In these languages, characters may be represented by two or more bytes. Therefore, LENB provides a byte count, which is crucial for accurately measuring the size of text data in these double-byte languages. For single-byte languages, LENB typically returns the same value as LEN, but its real usage is in the context of DBCS, offering a precise byte-based count.

 

Use of TRIM with LEN Function for Accurate Character Calculation

Using the TRIM function in conjunction with LENGTH function in Excel is a common and useful practice, especially when dealing with data copied from browsers of other sources that may contain extra spaces. The combination of these two functions helps in obtaining a more accurate count of characters in a string.

For example, you have a cell containing the text string "Hello World   ". It contains 3 extra space characters in the end. It is difficult to see those space characters to a user viewing the Excel file. The raw character count using LEN would include all spaces, leading to an inflated count of 14 characters. However, if you first apply TRIM to remove the unnecessary spaces, the LEN function will then give you the correct count of 11 characters.

=LEN("Hello World   ") will return 14

=LEN(TRIM("Hello World   ")) will return 11

 

Error Handling in LEN Function

Error handling in the context of Excel's LEN function is relatively straightforward, as the function itself is quite simple and designed to count the number of characters in a given string. However, there are a few scenarios where you might encounter errors or unexpected results, and understanding how to handle these situations can be crucial for accurate data processing.

Handling Hidden Characters: Hidden characters such as spaces, tabs, or line breaks can also be counted by the LEN function, which might lead to unexpected character counts. Use functions like TRIM to remove unwanted spaces, or other text functions to clean up the text before using LEN.

Dealing with Errors in Referenced Cells: If the LEN function references a cell that contains an error, it will propagate that error instead of returning a character count. You can use the IFERROR function to handle this. For example:

=IFERROR(LEN(A1), "Error in Cell")

would return a custom message instead of an error.

#VALUE! Error with Incorrect Data Type: If you mistakenly reference a cell with a data type that LEN cannot handle (like an image or a chart), the function will return #VALUE! error. Always ensure that the cell referenced contains text, numbers, or date values.

Large Strings: While unlikely, if you try to use LEN on a string that exceeds Excel's cell character limit (32,767 characters), it may not return the correct count of characters. Be cautious when working with very large strings, and consider breaking them into smaller segments if necessary.

How to Use LEN Function to Calculate Word Count

Using the LEN function in Excel to calculate the word count in a text string is requires a combination of several functions. The process involves counting the total number of characters in a string (including spaces) and subtracting the number of characters without spaces, then adding 1. This method hinges on the idea that the number of words in a sentence is one more than the number of spaces for standard, space-separated text. To implement this, you first use the LEN function to get the total number of characters in the text, including spaces. Then, you use LEN again in conjunction with the SUBSTITUTE function to count the characters without spaces (by replacing spaces with nothing). The difference between these two counts, plus one, gives you the word count.

For example, consider the text string "Hello World from SpreadsheetWeb". You would use the formula:

=LEN(A1) - LEN( SUBSTITUTE(A1," ","") ) + 1

Here, LEN(A1) calculates the total number of characters in the string (including spaces), and LEN(SUBSTITUTE(A1," ","")) calculates the number of characters without spaces. The difference gives the number of spaces, and adding 1 yields the word count. This method works reliably for text with words separated by single spaces. However, it's important to ensure the text doesn't have extra spaces between words or at the beginning or end of the string. In such cases, using the TRIM function to clean the text before applying this formula is advisable for accurate word count. This approach demonstrates a creative use of Excel's functions to perform a task that goes beyond their standard applications, showcasing the versatility of Excel in text data manipulation.

Tips

  • The Excel LEN function counts space characters.
  • LEN counts decimal separators ("." In US, "," in Europe).
    • =LEN(1984.12) returns 7 in US
  • Thousand separators are not counted, since they are used for number formatting.
  • Excel evaluates date values as a number, starting from 1. The 1 represents the date 1/1/1900, so a current date like 4/17/2019 is equal to 43572. The LEN function counts the number of digits of the serial number which is 5 in this case, not formatted date.
  • Note that the TEXT function returns a string that contains currency symbols or thousand separators as characters, not as an element of number formatting. As a result, LEN function counts symbols and thousand separators as well. For example:
    • =LEN(TEXT(1984.12,"#,##0.00")) returns 8