Checking for Digits in a String

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


1

Gary has a macro that uses InputBox for the user to input a string. The string can be just about any length. He needs to check the string to make sure it doesn't contain any digits. (Letters are OK, but no digits.) Gary wonders if there an easy way to do this.

There are actually several easy ways to do this. Since Gary is doing his work in a macro, it is probably best to demonstrate the different ways by creating a function that returns False if there are no digits in the string and True if there are digits. Gary could call the function right after getting the user input, and then take some action based on what is returned.

Here is an example of the VBA code you can use when you want to check if entered string contains a digit:

Function CheckInput(sRaw As String) As Boolean
    Dim J As Integer

    CheckInput = False    ' Assume no digits

    For J = 1 To Len(sRaw)
        If IsNumeric(Mid(sRaw, J, 1)) Then
            CheckInput = True
            Exit For
        End If
    Next J
End Sub

This approach is fairly typical of checking—it steps through each character in the string being checked, and if the character is a digit (determined by the IsNumeric function), then the checking is exited and CheckInput returns True.

If the strings being entered are routinely long, then it may be beneficial to turn the checking on its head, in this manner:

Function CheckInput(sRaw As String) As Boolean
    Dim J As Integer

    CheckInput = False    ' Assume no digits

    For J = 0 To 9
        If InStr(sRaw, J) <> 0 Then CheckInput = True
    Next J
End Sub

This approach only requires 10 checks of the string, using the digits 0 through 9. In this case, InStr is used to see if the digit is anywhere in the string being checked. If so, then CheckInput is set to True. Notice, as well, that this approach doesn't use an Exit For statement because ten passes through the string can be executed quickly.

You could make the checking function even shorter by relying on the Like operator, as in the following:

Function CheckInput(sRaw As String) As Boolean
    CheckInput = sRaw Like "*#*"
End Sub

Notice that the check requires only a single line. The Like operator always returns True or False based on a pattern that is compared against, in this case, the sRaw string. The pattern specified for the Like operator is any number of characters followed by a digit followed by any number of characters. Thus, if there is even a single digit in the string, then CheckInput is set to True.

There is another advantage to using the Like function—you can limit what characters will cause the test to fail. Here, for instance, is a variation that only returns True if the string contains the digits 3 through 7.

Function CheckFor37(sRaw As String) As Boolean
    CheckFor37 = sRaw Like "*[3-7]*"
End Sub

This means that the string being checked can contain 0, 1, 2, 8, or 9. It is only 3, 4, 5, 6, and 7 that cause the function to return True.

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 (10276) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, 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

Changing the Perspective of Your Chart

Microsoft Graph can be a handy way to add quick and dirty charts to your document. When working with 3-D charts, you can ...

Discover More

Printing Multiple Envelopes

Need to print envelopes quite often? An easy way to do it is to create an envelope template, as described in this tip.

Discover More

Clearing and Deleting Cells

When you want to remove information from a worksheet, you can either clear cells or delete cells. This tip examines the ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!

More ExcelTips (ribbon)

Finding the Last-Used Cell in a Macro

Ever wonder what the macro-oriented equivalent of pressing Ctrl+End is? Here's the code and some caveats on using it.

Discover More

Understanding Functions in Macros

Functions are a common programming construct. They help you to create easy ways of processing information and returning a ...

Discover More

Extracting Proper Words

If you've got a list of potential words, and you want to know which of those potential words are real, you'll appreciate ...

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 six less than 6?

2025-05-04 10:31:00

J. Woolley

For related discussion, see https://tips.net/T012654#comment-form-hd


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.