Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Accepting Only a Single Digit

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.

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.

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.

Related Tips:

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!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Robert Menucci    16 Feb 2016, 12:53
I used to use Excel everyday, but then changed jobs which used a different Estimating Software, so I got out of practice, especially since I retired.
My question is;
There is a shortcut key to copy the contents of the above, or, previous cell. I just can't find the answer. Maybe that has changed in later versions.
Thanks in advance, I will take the answer off air.
Bob
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.