The validation feature in Excel is great, but there is one key thing it cannot do (without the following hack): automatically add a new entry to the list being used as the source for the validation list.
If you have used validation, you know it's a neat feature. Perhaps most impressive is its ability to add a list to any cell from which the user can then select. Wouldn't it be nice if, when you enter a new name in a validated cell, Excel automatically adds it to the list? This is possible, thanks to the following hack.
Assume you have a list of names in the range A1:A10, as in Figure 2-24.
These names represent employees in a company. It is not uncommon for new employees to be added to such a list, but at present, the only way to achieve this is to add the new names to the end of the list and then select the new names from the list in the validated cell.
To overcome this limitation, follow these steps. In cell A11, enter the following formula and copy it down to row 20, as in Figure 2-25 (note the relative reference of A10):
=IF(OR($D$1=",COUNTIF($A$1:A10,$D$1)),"x",$D$1)
Now select Insert
=OFFSET(Sheet1!$A$1,0,0,COUNTIF(Sheet1!$A:$A,"<>x"),1)
Select cell D1 and then select Data
Right-click the Sheet Name tab and select View Code. Enter the following code:
Private Sub Worksheet_Calculate( ) On Error Resume Next Application.EnableEvents = False Range("MyNames") = Range("MyNames").Value Application.EnableEvents = True On Error GoTo 0 End Sub
Close the window to get back to Excel and save your workbook. Now select cell D1, type in any name that is not part of the list, and press Enter. Select cell D1 again and look at the list. The new name should be part of it, as shown in Figure 2-28.
If you want to add more than 10 names to your list, just copy the formula down past row 20.