Written by Allen Wyatt (last updated February 6, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003
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.
Note:
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.
Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!
You can edit cell information either in the Formula bar or in the cell itself. Here's how you can configure Excel to ...
Discover MoreWant a quick way to tell how may rows and columns you've selected? Here's what I do when I need to know that information.
Discover MoreEach time you take some action in Excel, the action is saved in an "undo stack" so that the action can be undone, if ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments