Validating the data. Microsoft Excel VBA Programming
Pubblicità
Pubblicità
Chapter 18: UserForm Techniques and Tricks
295
Figure 18-3:
Use the custom dialog box for data entry.
Here’s how the EnterButton_Click procedure works:
✓ in column A and to determine the next blank cell in the column.
✓ was selected and writes the appropriate text (Male, Female, or Unknown) to column B.
✓ that clicking the Enter button doesn’t close the dialog box, because the user will probably want to enter more data. To end data entry, click the
Close button.
Validating the data
Play around with this routine some more and you find that the macro has a small problem: It doesn’t ensure that the user actually enters a name into the
TextBox. The following code — which is inserted in the EnterButton_Click procedure before the text is transferred to the worksheet — ensures that the user enters some text in the TextBox. If the TextBox is empty, a message appears and the routine stops.
‘ Make sure a name is entered
If TextName.Text = “” Then
MsgBox “You must enter a name.”
Exit Sub
End If
Scarica
Pubblicità