|
To enter this Macro you have to follow the steps below: 1. Open the Visual Basic editor by selecting Tools / Macro / Visual Basic Editor (or pressing Alt + F11 ) 2. Insert a new module by selecting Insert / Module and copy the following statements to the active code window:
Sub DataValidation() Dim I As Integer ' Check that each cell contains positive values and < 500 For I = 1 To 10 If Cells(I, 2) <= 0 Or Cells(I, 2) > 500 Then Cells(I, 2).Select GoTo ErrorMessage End If Next I ' Check that the total is < 3,000 If Cells(11, 2) > 3000 Then Cells(11, 2).Select GoTo ErrorMessage End If ' If data is OK MsgBox "The data entered is OK." Cells(30, 1).Select Exit Sub ' If data is not OK ErrorMessage: MsgBox "The data entered is not valid." End Sub
3. Close the Visual Basic editor and return to Excel. Enter data and run the Macro. You can run the Macro by using the following methods: A. Use the normal Excel menu by selecting: Tools / Macro / Macros / DataValidation / Run B. Assign the Macro to a button of the Forms Toolbar (as shown in the figure above). The latter can be done by following the steps below: (a) Load the Forms toolbar. To do this Right click on a tool bar and select "Forms" from the shortcut menu. (b) Click on the Button icon and use standard mouse techniques to place it on the worksheet (c) In the "Assign Macro" window that appears, select the macro "DataValidation" and press OK.
You can download the above example from this link.
|
|