Identifying Digit-Only Part Numbers Excluding Special Characters

Written by Allen Wyatt (last updated March 23, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365


4

Chris has a large number of cells that contain part numbers. These cells can contain either digits or characters, in any combination. They can also contain special characters such as dashes, slashes, and spaces. Chris needs a way to identify if a cell contains only digits, without taking the special characters into account. Thus, a cell containing 123-45 would show as containing only digits, while 123AB-45 would not.

The easiest way to figure out if a given cell contains only the allowable characters and digits is to use a formula that removes the non-digit permissible characters and then sees if the resulting value is numeric. All of the following formulas can do the trick quite nicely:

=IF(IFERROR(INT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"-", ""),"/", "")," ", "")),FALSE), TRUE, FALSE)
=OR(ISNUMBER(SUBSTITUTE(A1,"-","")+0),ISNUMBER(SUBSTITUTE(A1,"/","")+0),ISNUMBER(SUBSTITUTE(A1," ","")+0))
=ISNUMBER(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," ",""),"/",""),"-","")*1)

You could also use a simple macro to figure out if a cell contains only digits and your allowed characters. While there are any number of ways that such a macro could be approached, here's a rather easy method:

Function DigitsOnly(sRaw As String) As Boolean
    Dim X As Integer
    Const sAllowed As String = "0123456789 -/"

    Application.Volatile
    For X = 1 To Len(sRaw)
       If InStr(sAllowed, Mid(sRaw, X, 1)) = 0 Then Exit For
    Next X
    DigitsOnly = False
    If X > Len(sRaw) Then DigitsOnly = True
End Function

The macro examines whatever is passed to it, comparing each character in the string to a list of allowed characters (in the constant sAllowed). If a disallowed character is detected, the loop is exited early and a False value is returned. Thus, if you wanted to evaluate the cell at A1, you could use the following in your macro:

=DigitsOnly(A1)

Since they return either True or False values, any of these approaches (formula or user-defined function) could be used in conjunction with conditional formatting to make formatting changes to your part numbers.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12654) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

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. ...

MORE FROM ALLEN

Inserting Video into Worksheets

You can add all sorts of objects to your workbooks, including video clips. Here's the pros and cons (along with the ...

Discover More

Roman Numerals for Page Numbers

Yes, Excel can work with Roman numerals, and it even provides a worksheet function that converts to them. How you use ...

Discover More

Cannot Combine Two Tables

When working with tables, a common editing task is to combine two tables into one. Sometimes, though, you may run into ...

Discover More

Best-Selling VBA Tutorial for Beginners Take your Excel knowledge to the next level. With a little background in VBA programming, you can go well beyond basic spreadsheets and functions. Use macros to reduce errors, save time, and integrate with other Microsoft applications. Fully updated for the latest version of Office 365. Check out Microsoft 365 Excel VBA Programming For Dummies today!

More ExcelTips (ribbon)

How Operators are Evaluated

Operators are used in formulas to instruct Excel what to do to arrive at a result. Not all operators are evaluated in the ...

Discover More

Condensing Sequential Values to a Single Row

If you have a bunch of ZIP Codes or part numbers in a list, you may want to "condense" the list so that sequential series ...

Discover More

Saving Common Formulas

Got some formulas you slaved over and want to use in lots of workbooks? This tip presents some helpful ideas on how you ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is three more than 2?

2024-03-28 15:26:35

J. Woolley

Re. my most recent comment below, VBA's Like operator supports limited patterns but a regular expression has more features. Therefore, all these cell formulas match the Tip's DigitsOnly(A1) result
    =NOT(IsRegEx(A1,"[^\d\s/-]+"))
    =NOT(IsRegEx(A1,"[^0-9 /-]+"))
    =NOT(IsRegEx(A1,"[^0123456789 /-]+"))
    =NOT(IsLike(A1,"*[!0123456789 /-]*"))
    =NOT(IsLike(A1,"*[!0-9 /-]*"))
See https://www.tutorialspoint.com/vbscript/vbscript_reg_expressions.htm
and https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/like-operator


2024-03-27 17:49:27

J. Woolley

My Excel Toolbox now includes the following IsRegEx function for convenient use of VBScript.RegExp to compare Text with a regular expression Pattern:

Public Function IsRegEx(Text As String, Pattern As String) As Boolean
    With CreateObject("VBScript.RegExp")
        .Pattern = Pattern
        IsRegEx = .Test(Text)
    End With
End Function

Reviewing my previous comment below, these cell formulas match the Tip's DigitsOnly(A1) result
    =NOT(IsRegEx(A1,"[^0123456789 /-]+"))
    =NOT(IsLike(A1,"*[!0123456789 /-]*"))
and these cell formulas help Chris identify cells with part numbers having one or more digits but no letters
    =AND(IsRegEx(A1,"\d+"),NOT(IsRegEx(A1,"[a-zA-Z]")))
    =AND(IsLike(A1,"*#*"),NOT(IsLike(A1,"*[a-zA-Z]*")))
See https://sites.google.com/view/MyExcelToolbox
and https://www.tutorialspoint.com/vbscript/vbscript_reg_expressions.htm


2024-03-25 20:48:02

Peter

The regular expression [^\d] matches any data that contains a character that is NOT a digit. This can be implemented with a macro:

Function NotDigits(PartNum) As Boolean
Dim regex
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "[^\d]"
NotDigits= regex.Test(PartNum)
End Function


2024-03-24 10:23:00

J. Woolley

Here is a another version of the Tip's DigitsOnly function:

Function DigitsOnly2(Text As String) As Boolean
    DigitsOnly2 = Not Text Like "*[!0123456789 /-]*"
End Function

And here is a more general version that returns TRUE only when a cell's text includes a digit and does not include a letter; Chris might find this more useful:

Function DigitsOnly3(Text As String) As Boolean
    DigitsOnly3 = Text Like "*#*" And Not Text Like "*[a-zA-Z]*"
End Function

My Excel Toolbox includes the following IsLike function for convenient use of VBA's Like operator in a cell formula:

Public Function IsLike(Text As String, Pattern As String) As Boolean
    IsLike = Text Like Pattern
End Function

Therefore, this cell formula matches the result from DigitsOnly2(A1)
    =NOT(IsLike(A1,"*[!0123456789 /-]*"))
and this cell formula matches the result from DigitsOnly3(A1)
    =AND(IsLike(A1,"*#*"),NOT(IsLike(A1,"*[a-zA-Z]*")))
See https://sites.google.com/view/MyExcelToolbox


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.