Loading

Got a version of Excel that uses the
menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)?
**This site is for you!** If you
use a later version of Excel, visit
our *ExcelTips* site focusing on the ribbon interface.

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company.

Learn more about Allen...

ExcelTips FAQ

ExcelTips Resources

Ask an Excel Question

Make a Comment

Free Business Forms

Free Calendars

** Please Note:** This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later),

Tony has a bunch of data in a worksheet that consists of digits and other characters. For instance, he might have a cell that contains "1234567Blue." Tony wants to be able to figure out the character position at which the first non-digit character occurs. In the example of the text "1234567Blue" Tony wants some way to figure out that the first non-digit character is at position 8.

There are two primary ways to get the value you want. The first is to use an array formula to calculate the position. The following array formula (entered by using **Ctrl+Shift+Enter**) will work in the majority of cases:

=MATCH(TRUE,ISERROR(VALUE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))),0)

The only instances where this array formula won't work is if cell A1 is either empty or contains a strictly numeric value. If your list may contain this type of data (or no data at all), then you should consider using a slightly longer array formula:

=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)

Remember that that is a single array formula, entered by using **Ctrl+Shift+Enter**. It will properly handle instances where A1 contains no non-digit characters (as in a blank cell or a value such as "123").

Of course, the other way you can handle finding out the position of the first non-digit character is to create a user-defined function. There are many different ways that such a macro can be implemented. One of the easiest ways to implement the macro is to simply step through each character in whatever is passed to the macro. When a character is found that is outside the ASCII code range for digits (48 to 57), then you know you've found the first position. The following macro shows a way to do this type of technique:

Function FirstNonDigit(str As String) Dim iChar As Integer Dim iPos As Integer Dim J As Integer Application.Volatile iPos = 0 For J = 1 To Len(str) iChar = Asc(Mid(str, J, 1)) If iChar <= 47 Or iChar >= 58 Then iPos = J Exit For End If Next J FirstNonDigit = iPos End Function

To use the function, simply use a formula such as this in your worksheet:

=FirstNonDigit(A1)

If the cell you reference is empty or if it only contains digits, then the function returns a 0 value.

*ExcelTips* is your source for cost-effective Microsoft Excel training. This tip (3364) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Finding the First Non-Digit in a Text Value.

*Related Tips:*

**Professional Development Guidance!** Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out *Professional Excel Development* today!

@Micky: Exit For is clearer to me. It means: we found what we were looking for, so terminate the loop. And since the difference in speed is as good as unmeasurable...

@Willy Vanhaelen,

To my opinion the use of: Exit Function, is more appropriate.

--------------------------

Michael (Micky) Avidan

“Microsoft® Answers" - Wiki author & Forums Moderator

“Microsoft®” MVP – Excel (2009-2017)

ISRAEL

To my opinion the use of: Exit Function, is more appropriate.

--------------------------

Michael (Micky) Avidan

“Microsoft® Answers" - Wiki author & Forums Moderator

“Microsoft®” MVP – Excel (2009-2017)

ISRAEL

I always prefer a user defined function (UDF) instead of those lengthy complicated formulas.

Here is a much shorter UDF that does the job equally well:

Function FirstNonDigit(str As String)

Dim J As Integer

For J = 1 To Len(str)

If Not IsNumeric(Mid(str, J, 1)) Then

FirstNonDigit = J

Exit For

End If

Next J

End Function

I left out Application.Volatile because it is not necessary here and has the disadvantage that the function gets recalculated each time you enter something in the sheet even in a cell that is not referenced by the function. This applies also to INDIRECT used in the formulas in this tip.

Here is a much shorter UDF that does the job equally well:

Function FirstNonDigit(str As String)

Dim J As Integer

For J = 1 To Len(str)

If Not IsNumeric(Mid(str, J, 1)) Then

FirstNonDigit = J

Exit For

End If

Next J

End Function

I left out Application.Volatile because it is not necessary here and has the disadvantage that the function gets recalculated each time you enter something in the sheet even in a cell that is not referenced by the function. This applies also to INDIRECT used in the formulas in this tip.

Thanks