Userform Clear Controls

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
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
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
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi..
Maybe something like..

Code:
If TextName.Text = "" Then       
 MsgBox "You must enter a name."
Elseif TextName.Text <> "" then
Cells(NextRow, 1) = TextName.Text
End if
 
Upvote 0
Hi,


maybe this:

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
        If TextName.Text = "" Then
            GoTo flow_handler
        End If
        '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
flow_handler:
        MsgBox "You must enter a name."
        Exit Sub
        
End Sub
 
Upvote 0
Hy, Sorry for the late response.... Apo/Jtufplata: I tried your code. Both still show up the msgbox right after I press the Ok button. The controls are cleared but the msgbox shows up immediatly.
 
Upvote 0
IN both versions they will always appear if rownum+1 is "", which apparently always is. Just remove the msgbox option, and change your code so no null entries will be created
 
Upvote 0
you need to test for a null string from your textbox before the rest of the code is executed.


see if this approach helps:

Code:
Private Sub EnterButton_Click()
    Dim NextRow As Long

    'Make sure a name is entered
    If TextName.Text = "" Then
        MsgBox "You must enter a name."
    Else
        '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 gender
        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
    End If
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,216,110
Messages
6,128,892
Members
449,477
Latest member
panjongshing

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top