Oprichnick
Board Regular
- Joined
- May 30, 2013
- Messages
- 69
Hy, I bought a Excel Vba book which has several code samples for userforms. But I think there is some flaw in the code I show you below
When you enter values, the controls are cleared as it should, however the msgbox "You must enter a name" appears instantly as it not should. I understand where is the flaw, but I can't solve it. Kind Regards
Code:
Private Sub EnterButton_Click()
Dim NextRow As Long
'Make sure Sheet1 is active
Sheets("Folha1").Activate
'Determine the next empty row
NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer the name
Cells(NextRow, 1) = TextName.Text
'Transfer the sex
If OptionMale Then Cells(NextRow, 2) = "Male"
If OptionFemale Then Cells(NextRow, 2) = "Female"
If OptionUnknown Then Cells(NextRow, 2) = "Unknown"
'Clear the controls for the next entry
TextName.Text = ""
OptionUnknown = True
TextName.SetFocus
'Make sure a name is entered
If TextName.Text = "" Then
MsgBox "You must enter a name."
Exit Sub
End If
End Sub