Pagebanner

ThreeWave Formulas For Working With Strings

This page describes some formulas for working with text strings.
ShortFadeBar

Introduction

This page describes a number of worksheet formulas that work with strings of text.

Counting The Number Of Specific Characters Or Strings Of Characters In A Cell

The following formula can be used to count the number of times that the character or string of characters in cell B1 occurs in the string in cell A1. For example, if A1 contains the string abcXdXeXf, and cell B1 contains the character x, the formula will return 3, since there are 3 'x' characters in A1. This formula does not distinguish between upper and lower case. Cell B1 may contain multiple characters. In this case, the formula counts the number of times that string occurs in cellA1, not the number of times each individual characters in cell B1 occur. For example, if B1 contains abc, the formula counts the number of times the string abc occurs in cell A1, not the number of times the individual characters a, b, and c occur in cell A1.

=IF(LEN(B1)=0,0,(LEN(A1)-LEN(SUBSTITUTE(A1,B1,"")))/LEN(B1))

If B1 is empty, the formula returns 0. If you want to distinguish between upper and lower case, remove the UPPER functions from the formula. That is,

=IF(LEN(B1)=0,0,(LEN(A1)-LEN(SUBSTITUTE(A1,B1,"")))/LEN(B1))

SectionBreak

Counting The Number Of Letters In A Cell

The following formula counts the number of letters (A to Z, in either upper or lower case) in cell A1.

=IF(LEN(A1)=0,0,SUM(((CODE("A")<=CODE(UPPER(MID(A1,ROW(INDIRECT("A1:A"&LEN(A1))),1)))))*((CODE("A")<=CODE(UPPER(MID(A1,ROW(INDIRECT("A1:A"&LEN(A1))),1)))))))

This formula is an array formulas so you must press CTRL SHIFT ENTER rather than just ENTER when you first enter the formula and whenever you edit it later. If you do this properly, Excel will display the formula in the formula bar enclosed in curly braces { }. See the Array Formulas page for more information about array formulas. If A1 is empty, the result is 0.

SectionBreak

Counting The Number Of Digits In A Cell

The following formula counts the number of digits (0 to 9) in cell A1.

=IF(LEN(A1)=0,0,SUM(((CODE("0")<=CODE(MID(A1,ROW(INDIRECT("A1:A"&LEN(A1))),1))))*((CODE("9")>=CODE(MID(A1,ROW(INDIRECT("A1:A"&LEN(A1))),1))))))

This is an array formula so you must enter it with CTRL SHIFT ENTER rather than just ENTER. If cell A1 is empty, the formula returns 0.

SectionBreak

Position Of First Digit In A String

This formula will return the position of the first digit (0 - 9) in the string in A1.

=IF(LEN(A1)=0,0,MIN(IF(1*ISNUMBER(1*MID(A1,ROW(INDIRECT("A1:A"&LEN(A1))),1)),ROW(INDIRECT("A1:A"&LEN(A1))),LEN(A1)+1)))*OR(ISNUMBER(1*LEFT(A1,1)),ISNUMBER(1*RIGHT(A1,1)))

This is an array formula so you must enter it with CTRL SHIFT ENTER rather than just ENTER.

SectionBreak

Position Of First Non-Digit In A String

This formula will return the position of the first non-numeric character in the string in cell A1.

=IF(LEN(A1)=0,0,MIN(IF(1*ISNUMBER(1*MID(A1,ROW(INDIRECT("A1:A"&LEN(A1))),1))=0,ROW(INDIRECT("A1:A"&LEN(A1))),LEN(A1)+1)))*(ISNUMBER(A1)=FALSE)

This is an array formula so you must enter it with CTRL SHIFT ENTER rather than just ENTER.

SectionBreak

Position Of The Last Occurrence Of A Character In A String

The following formula will return the postion of the last occurrence of the character in cell B1 in the string in cell A1.

=MAX((MID(A1,ROW(INDIRECT("A1:A"&LEN(A1))),1)=B1)*ROW(INDIRECT("A1:A"&LEN(A1))))

This formula does not distinguish between upper and lower case. If you want to make this distinction, use the formula

=MAX((EXACT(MID(A1,ROW(INDIRECT("A1:A"&LEN(A1))),1),B1))*ROW(INDIRECT("A1:A"&LEN(A1))))

This is an array formula, so you must press CTRL SHIFT ENTER rather than just ENTER. If cell B1 is empty, the result is 0.

SectionBreak

Counting The Number Of Words In A Cell

The following formula will return the number of words in a cell. A word is considered by be a string of characters delimited by spaces. Other punctuation characters are not considered.

=IF(LEN(TRIM(A1))=0,0, LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))+1)

SectionBreak

String Concatenation

You can combine two string into a single string by using either the CONCATENATE function or the & operator. Unfortunately, neither of these can be used in an array formula to selectively build up a result string based on other criteria. See String Concatenation For Array Formulas for a VBA function that can be used in an array formula to build a string based on selection criteria.

This page last updated: 2-November-2007

Created by Chip Pearson at Pearson Software Consulting, LLC
Email: chip@cpearson.com Before emailing me, please read this page
http://cpearson.com/excel/stringformulas.aspx
Copyright © 1997 - 2009, Charles H. Pearson

Submit bug information or errors on the Bug And Error Report Page.



 


sectionbreak

Essential Tools For Developers

Add-in Express, true RAD tool for developing Office extensions


Essential Tools For Financial Analysts And Accounting Professionals

  
Ready


Advertise Your Product On This Site


SectionBreak