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), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Accepting Only a Single Digit.

Accepting Only a Single Digit

Written by Allen Wyatt (last updated January 11, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


3

Rich wonders how he can configure Excel so that when he enters a single digit it will automatically advance to the next cell. He wants to eliminate hitting Enter or Tab to get to the next cell. The value of the entry for a range of cells will always be a single positive digit.

This cannot be done with any native configuration setting in Excel. Instead, you will need to create a macro that will handle the entry for you. A natural choice for the macro is to use the Change event for the worksheet, so that any time a value is entered into a cell, the entry is "pulled apart" and stuffed in cells in the row.

Private Sub Worksheet_Change(ByVal Target As Range)
    If IsNumeric(Target.Value) Then
        CRow = Target.Row
        CColumn = Target.Column - 1
        Entry = Target.Value
        For i = 1 To Len(Entry)
            Cells(CRow, CColumn + i).Value = Mid(Entry, i, 1)
        Next
    End If
End Sub

This macro checks, first, to see if what was entered is numeric. If it is, then the digits are extracted from the value and placed in consecutive cells in the row.

The drawback to such a macro, of course, is that you still need to press Enter to trigger the event. If you want to get away from pressing Enter entirely, then you will need to rely upon a different approach. This technique relies upon the OnKey function to assign macros to specific keystrokes. Place the following code into a standard macro module.

Sub Assigns()
    Dim i As Variant
    With Application
        For i = 0 To 9
            .OnKey i, "dig" & i
        Next
    End With
End Sub
Sub ClearAssigns()
    Dim i As Variant
    With Application
        For i = 0 To 9
            .OnKey i
        Next
    End With
End Sub
Sub dig0()
    ActiveCell.Value = 0
    ActiveCell.Offset(1, 0).Select
End Sub
Sub dig1()
    ActiveCell.Value = 1
    ActiveCell.Offset(1, 0).Select
End Sub
Sub dig2()
    ActiveCell.Value = 2
    ActiveCell.Offset(1, 0).Select
End Sub
Sub dig3()
    ActiveCell.Value = 3
    ActiveCell.Offset(1, 0).Select
End Sub
Sub dig4()
    ActiveCell.Value = 4
    ActiveCell.Offset(1, 0).Select
End Sub
Sub dig5()
    ActiveCell.Value = 5
    ActiveCell.Offset(1, 0).Select
End Sub
Sub dig6()
    ActiveCell.Value = 6
    ActiveCell.Offset(1, 0).Select
End Sub
Sub dig7()
    ActiveCell.Value = 7
    ActiveCell.Offset(1, 0).Select
End Sub
Sub dig8()
    ActiveCell.Value = 8
    ActiveCell.Offset(1, 0).Select
End Sub
Sub dig9()
    ActiveCell.Value = 9
    ActiveCell.Offset(1, 0).Select
End Sub

To start the macro, run the Assigns macro. Thereafter, every time a digit is typed the digit is stuffed into the current cell and the next cell to the right selected. If you type in text, then nothing happens. (Of course, if you try to enter a mixed value, such as B2B, then when you press "2" that is what will end up in the cell.) When you are done with this type of data entry, run the ClearAssigns macro to finish up.

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 (6614) 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: Accepting Only a Single Digit.

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

Clearing the Contents of a Table

Want to get rid of information within a table, but not the table itself? Here's a guide to understanding the effects that ...

Discover More

Turning Off ScreenTips

ScreenTips are one of those artifacts of Microsoft trying to make Excel be overly helpful. If the ScreenTips bother you, ...

Discover More

Putting a Different Date in a Header

Today's date is easy to add to a header, but what if you want to add a date that is adjusted in some manner? Adding ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (menu)

Jumping to the Start of the Next Data Entry Row

Want a quick way to jump to the end of your data entry area in a worksheet? The macro in this tip makes quick work of the ...

Discover More

Modifying Default Year for Dates

When entering dates into a worksheet, you may want the dates to default to last year instead of this year. Here's a way ...

Discover More

Entering Large Time Values

If you need to input humongous times into a worksheet, you may run into a problem if you need to enter times greater than ...

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 two more than 7?

2020-05-19 19:59:59

Jack

Hi Allen,

Thanks for the tips.

I tried it and it works with "0" only, when I input number from 1 - 9 in the cell, I got below.

Cannot run the macro. “The macro may not be available or all macros may be disabled”

Any advice?

Thanks
Jack


2020-05-19 14:29:52

Jack

Hi Allen,

Thanks for the tips.

I tried it and it works with "0" only, when I input number from 1 - 9 in the cell, I got below.

Cannot run the macro. “The macro may not be available or all macros may be disabled”

Any advice?

Thanks
Jack


2020-01-12 12:02:23

alex martinez

all the one digit numbers in a1; numbers 1 to any number 123456 ... in cell b1 & =MID(A$1,B1,1), =MID(A$1,B2,1), etc. in column c1, you enter all the one digit numbers in a1 and hit enter once
a1 b1 c1 c1
654789 1 6 =MID(A$1,B1,1)
2 5
3 4
4 7
5 8
6 9


This Site

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.

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.