programming style question

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I have a simple UserForm. It has 2 textboxes (myname, myage) and 2 option buttons (male, female) and 1 control button (enterme) . Please see the code below.

What I want to add is to give a user a message, if the user forget to enter Name, age, and sex and hit Enter button. So I came with the following idea to fix the issue if the user did not enter name

Code:
If myname.Value = "" Then
        MsgBox "enter name"
    End If

How you would do that? is that good programming style? Thank you.

Code:
Private Sub enterme_Click()
    Dim x As Double
    x = Cells(Rows.Count, 1).End(xlUp).Row
    If myname.Value = "" Then
        MsgBox "enter name"
    End If
    Cells(x + 1, 1).Value = myname.Value
    Cells(x + 1, 2).Value = myage.Value
    If male.Value = True Then
        Cells(x + 1, 3).Value = "male"
    ElseIf female.Value = True Then
        Cells(x + 1, 3).Value = "female"
    End If
End Sub
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Not sure about programming style but you probably want to add an Exit Sub to exit the Click procedure if the user hasn't entered a name.
Code:
    If myname.Value = "" Then
        MsgBox "enter name"
        Exit Sub
    End If
 
Upvote 0
If you wanted for force the user to enter and choose all the values you want
Try this:
Code:
Private Sub enterme_Click()
    'Modified  11/28/2018  6:45:19 PM  EST
    Dim x As Double
    x = Cells(Rows.Count, 1).End(xlUp).Row
    If myname.Value = "" Then MsgBox "I'm going to stop the script till you enter a name": Exit Sub
    If Myage.Value = "" Then MsgBox "I'm going to stop the script till you enter an age": Exit Sub
    If Male.Value = False And Female.Value = False Then MsgBox "I'm going to stop the script till you choose Male or Female": Exit Sub
    Cells(x + 1, 1).Value = myname.Value
    Cells(x + 1, 2).Value = Myage.Value
    If Male.Value = True Then
        Cells(x + 1, 3).Value = "male"
    ElseIf Female.Value = True Then
        Cells(x + 1, 3).Value = "female"
    End If
    Male.Value = False
    Female.Value = False
    myname.Value = ""
    Myage.Value = ""
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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