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: Automatically Moving from Cell to Cell when Entering Data.

Automatically Moving from Cell to Cell when Entering Data

by Allen Wyatt
(last updated August 26, 2016)

5

Sheila has a worksheet in which a series of four-digit numbers needs to be entered. She would like a way where Enter or Tab doesn't need to be pressed between each entry. In other words, after each fourth digit is pressed, Sheila wants Excel to automatically advance to the next cell.

Excel does not provide this type of data entry as an option. You can, however, create a macro to handle the data entry. One way is to use a simple macro that prompts the user for a string of characters. When the user presses Enter (to signify that the string is complete), then the macro takes each successive four-character chunk and puts them in consecutive cells.

Sub FourCharEntry1()
    Dim str As String
    Dim x As Integer
    Dim y As Integer

    str = InputBox("Enter string")
    y = 0
    For x = 1 To Len(str) Step 4
        ActiveCell.Offset(0, y) = "'" & Mid(str, x, 4)
        y = y + 1
    Next
End Sub

Notice that the macro, as it is putting four-character chunks into cells, makes sure that each chunk is preceded by an apostrophe. The reason for this is to handle those instances when the four-character chunk may consist of only numbers and those numbers begin with one or more zeroes. Adding the apostrophe makes sure that Excel treats the cell entry as text and the leading zeroes won't be wiped out.

You could, as well, avoid the use of an InputBox by simply allowing someone to enter text into a cell in the worksheet. The person could type away as much as desired (thousands of characters, if necessary). Then, with the cell selected, you could run a macro that will pull the information from the cell and perform the same task—breaking it up into four-character chunks. The following macro does just that:

Sub FourCharEntry2()
    Dim str As String
    Dim x As Integer
    Dim y As Integer

    str = ActiveCell.Value
    y = 0
    For x = 1 To Len(str) Step 4
        ActiveCell.Offset(0, y) = "'" & Mid(str, x, 4)
        y = y + 1
    Next
End Sub

Another approach is to use a custom user form for the user input. The form provides a much richer interaction with VBA, so you can actually have it stuff information into cells after every fourth character is entered.

Start by creating a user form (as described in other issues of ExcelTips) that contains two controls—a text box and a button. Name the text box vText and associate the following code with it:

Private Sub vText_Change() 
    If Len(vText) = 4 Then
        ActiveCell = vText
        ActiveCell.Offset(0, 1).Activate
        UserForm1.vText.Value = ""
    End If
End Sub

This simply runs every time the contents of the text box change (i.e., when you type each character) and then checks the length of whatever it contains. When the length reaches 4 the code takes those characters and stuffs them into a cell. The contents of vText are then emptied.

The name of the button you create in the user form doesn't really matter. It will be used as a way to close the user form, and should have the following code associated with it:

Private Sub Cancel_Click()
    Unload UserForm1
End Sub

When you are ready to use the user form, simply select the cell where you want input to start and then run the following macro:

Sub Start()
    UserForm1.Show
End Sub

The user form appears and you can start typing away. When you are done, just click the button and the user form is closed.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3923) 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: Automatically Moving from Cell to Cell when Entering Data.

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

Sharing AutoText Entries

AutoText entries can make your writing more productive, if used effectively. Because of this, it can be advantageous for all ...

Discover More

Checking for Proper Entry of Array Formulas

Excel allows you to enter two different types of formulas in a cell: A regular formula or an array formula. If you need to ...

Discover More

Rotating a Page of Text

Beginning with Word 2000, you can rotate a page of text by using the Far East language support built into Word. This tip ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

MORE EXCELTIPS (MENU)

Inserting a Radical Symbol

The radical symbol is used frequently in some branches of mathematics. If you want to insert a radical symbol in a cell, ...

Discover More

Undoing an Edit

We all make mistakes. Fortunately, Excel makes it rather easy to undo your makes, right after you make them.

Discover More

Deleting Duplicate Columns

Got a worksheet in which there may be entire columns that are duplicates of each other? If you want to delete those duplicate ...

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 for this tip:

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 9?

2017-03-01 11:37:23

Willy Vanhaelen

@Steinar

Simply swich the 0 and y in the Offset function:

ActiveCell.Offset(y, 0) = "'" & Mid(str, x, 4)


2017-02-28 16:41:51

Steinar

This is exactly what Im looking for! But I can't wrap my head around how to edit this macro into stuffing the input vertically instead of horizontally, as I lack the knowledge of what they individual parts of the code actually does. I guess this has something to do with the x and y axis, but trying to apply my common sense into this and modifying the code one letter at a time has taken me nowhere so far. I want to stack scanned input in a downwards motion.

Please explain this to me of refer this macro with a vertical function.


2016-08-16 05:14:03

GULAM

Sub FourCharEntry1()
Dim str As String
Dim x As Integer
Dim y As Integer

str = InputBox("Enter string")
y = 0
For x = 1 To Len(str) Step 4
ActiveCell.Offset(0, y) = "'" & Mid(str, x, 4)
y = y + 1
Next
End Sub


-if i apply this macro this will Put value in the same row.BUT I WANT TO PUT THE SPLIT VALUE IN NEXT ROW


2015-06-27 13:20:07

lmc

Trying to use Excel VBA code to move to a certain cell enter or change a formula, press enter, move to another cell in worksheet, enter or change a formula, press enter and etc. until all formulas are entered, move to next worksheet and repeat the code until all worksheets are completed in the workbook.


2014-04-17 22:33:27

Nick

I have a much simpler data entry but can't figure out how to change the code you've provided. I want to enter only "1" or "2" but automatically move down a column instead of across a row. This is for a test administration that will always have 467 items in it. So I need to input "1" or "2" for A1, A2, A3, A4...to A467. I've read your posts and switched the offset to (y, 0) but can't figure out the rest. Any help would be greatly appreciated. Thank you for this wonderful site.


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.

Links and Sharing