Your Search for Microsoft Excel tips and Tricks ends here.


Data Validation in Excel

Excel Worksheet Cell accepts values of any data type viz. Text, Number, Date/Time or expressions (result values can be any of these types).  Worksheets are the best tools for What…If analysis and present numerical data in Graph Chart form.  Besides all these we can create databases on worksheets and query information very quickly.

An Excel Database is like any other worksheet but created with few basic rules of databases in mind so that certain querying operations can be performed, like searching, filtering, lookup and so on.  I will list few basic rules, that you should know about setting up an excel based database (table) structure, below.

  1. The topmost row columns of a table should have the data Field Names and all of them should be unique (no duplicates) and cannot be in merged cells. 
  2. There should not be any column without column heading within the table area.
  3. Data type of each column should be of same type, like text or Number or Date or a formula that results in the same data type, not the mix of different data types in the same column.
  4. There should not be any blank rows within the table range, but some cells in a row can be empty, if needed.

These are some of the simple rules that goes for defining an excel based database. Image of a sample database is given below:

There are several built-in database functions in excel, like DSUM(), DAVERAGE(), DCOUNT() and others for analysis purposes, but the data must be reliable.  We have to ensure that the data entered into the data fields keep up with the strict rules that we set for each field.

A single cell under the First Name field can accommodate thousands of characters. We don’t need that much text in the First Name field.  But, we must be able to enter enough text (say a maximum of 25 characters) in the First Name field and about that much in the Last Name field as well, not more than that.

A list of validation rules applicable for each data field is given below. We will use these rules to perform validation checks on keyed-in data (result of expressions are not validated) before they are being accepted into the data field.

Data Validation Rules
SerialField NameData TypeLength Validation Rule
1. First Name Text 25 Text Data, maximum length 25 characters
2. Last Name Text 25 Text Data, maximum length 25 characters
3. Date Of Birth Date   Future Date and Date earlier than 100 Years not Valid
4. Age Number   Range 1 - 100

Now, let us try to implement the above rules in an excel Table area and experiment with it to see how these rules actually works while entering data into each column.

  1. Open a blank Excel Workbook.
  2. Enter the Table headings in Range A1 to D1 as shown in the image given above.
  3. Select the first cell below the column heading First Name.
  4. Select Data Validation . . . from the Data Tools Toolbar buttons group under the Data Menu. The Validation settings dialog control opens up like the image shown below:
  5. Select the Settings Tab.
  6. Select Text Length from the Allow: control’s drop-down list.
  7. Select Less than or equal to from the Data: control’s drop-down list.
  8. Type 25 into the Maximum control. The cell with this settings will allow only 25 characters of input into the cell.
  9. Select the Input Message control.

    When the User selects the Cell to key in data the settings on the Input Message control will appear as a comment box attached to the cell, to guide the user to enter a valid value, or with whatever message you intended to convey.

  10. Put check mark in the ‘Show input message when cell is selected’ control.  If check mark is not present then the message we set up in the next two steps will not appear on the worksheet.
  11. Type First Name in the Title control.
  12. Type Enter First Name (25 characters)  in the Input Message control.
  13. Select Error Alert Tab.  This is where we have to set up the real validation check to perform and alert the user when invalid data is entered into the cell.
  14. Put a check mark in the ‘Show Error Alert after invalid data is entered’. If this is not enabled then the user will not be warned on entry of invalid data.
  15. Select the Icon style (Stop, Warning or Information) of Alert message box, default stop will be just fine.
  16. Type First Name in the Title control.
  17. Type Maximum 25 characters.
  18. Click OK to close the dialog box.
  19. Select the first cell below Last Name field.
  20. Repeat Step-4 to Step-18 replacing First Name field name with Last Name, wherever applicable.
  21. Select the Cell beneath the Date Of Birth field.

    We will prevent this field from accepting a future date for Date of Birth.

  22. Select Data Validation option to open up the dialog control.
  23. Select Date from the drop-down list in the Allow: control.
  24. Select Less than or equal to in the Data: control.
  25. Type =TODAY() in the End Date: control.

    As you can see we can enter a Function or expression in this control rather than a constant value, when needed.

  26. Select the cell below the Age field.
  27. Enter the following expression to calculate the Age, based on the Date of Birth field value:

    We will leave this field from validation checks.  Validation checks can be performed only on cells where manual keying of data is done. 

  28. Highlight Cell Range A2:D2 and Right-Click on the highlighted range.
  29. Select Copy from the shortcut menu.
  30. Highlight Range A3:A20 Right-Click on the highlighted range.
  31. Select Paste Special from the shortcut menu.
  32. Select the Radio Button with the label Validation under the Paste options and click the OK button to apply the Validation rules we have copied from the source range A2:D2 into the target area A3:D20.
  33. Highlight Cell D2, right-click and select Copy from the shortcut menu to copy the Formula from cell D2.
  34. Highlight Range  D3:D20, right-click and select paste from the short-cut menu.

    Tip:  You may press Ctrl+C to copy the source cell and press Ctrl+V to paste the formula after highlighting the cell ranges.

  35. Save the Workbook file with a name.

Now, let us test our database’s validation checks.

  1. Type a name with more than 25 characters in length into Cell A2.  When the Error Alert pops up click Cancel and enter a name with 25 characters or less.
  2. Type something similar in Cell B2 with more than 25 characters.
  3. Type tomorrow’s date into Cell C2.

Key Words:Validation,Input Message,Error Alert


No comments:

Post a Comment


Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Featured Post

Copying a Worksheet into another Workbook

How to make a copy of a Worksheet from the current Workbook to another Workbook? Open the Target Workbook, when the Source Workbook is alr...

Recent Posts