Microsoft KB Archive/139663

= Visual Basic Example for Validating Data on Entry =

Article ID: 139663

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 5.0 Standard Edition

-



This article was previously published under Q139663





SUMMARY
To automate a task or perform an operation when data is entered into a specific range on a Microsoft Excel worksheet, you can use the OnEntry and Intersect methods in a Visual Basic for applications macro. You can use this type of macro to validate data entry, to verify that a value is within a specified range, for example.

NOTE: The macro provided below will run in Microsoft Excel 97 but you do not have to use the macro to validate data that is entered into cells. Microsoft Excel 97 has a new Data Validation feature that allows you to specify what data is valid for certain cells and ranges.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following sample macro generates the default system sound when valid data is entered in to a defined area of a worksheet. In this example, valid data is defined as a numeric value between the range of 0 and 100.

To use the following macros, use these steps to set up your Microsoft Excel workbook:  To define the range A1:A10 on Sheet1:  

 On Sheet1, select the range A1:A10. Point to Name on the Insert menu, and click Define. In the Names In Workbook box, type WatchArea, and click OK. </li> Insert a new module sheet into your workbook.</li>  In the module sheet, enter the following macros: Sub auto_open 'Set the WatchIT macro to run when data is entered into Sheet1 ThisWorkbook.Sheets("Sheet1").OnEntry = "WatchIT" End Sub

Sub WatchIT Dim isect As Excel.Range Set isect = Application.Intersect(Range(ActiveCell.Address), _           Range("WatchArea")) If isect Is Nothing Then 'Do Nothing Else 'You just entered into the defined area "WatchArea" on          '"Sheet1" add other the desired action code here or call 'another routine. 'Example below will alert the user if the data value is less 'than 0 or greater than 100 or is not a number and clear the 'entry. It will also give a confirmation beep when valid 'data is entered. If (Val(ActiveCell.Value) < 0 _              Or Val(ActiveCell.Value) > 100) _ Or Not IsNumeric(ActiveCell.Value) Then ActiveCell.Clear MsgBox "The data value must be a Number between 0 and 100" End If        Beep End If     End Sub NOTE: To customize this macro, change the "Sheet1" sheet name to the name of the sheet you want to "watch." Also, modify the "WatchArea" name in the WatchIT macro to the name of the defined area on the sheet specified in the auto_open routine. Use the Application.OnEntry code to "watch" data entry in all open workbooks and worksheets. You can also use the WorkBooks collection to qualify a specific workbook if desired. </li> Save the workbook, close and re-open it.</li></ol>

This will run the Auto_Open macro that sets an internal flag for Microsoft Excel to run the WatchIT macro whenever you enter data into the "WatchArea" range of cells.

When you enter appropriate data in the range A1:A10 on Sheet1, you will hear the default system sound. If you enter data that is outside the specified range (in this case any value that is not in the range 0- 100), a dialog box will be displayed.

<div class="references_section">