Excel 2010 - Testing Textbox contents. "Less than zero" or "nothing was input into the textbox"

I have a textbox and would like to test the contents for values less than or equal to zero, or if the field was not even visited by the user

If Val(SalaryEarnings.Value) <= 0 Or ___________

Open in new window


What do I put where the underline is?  ""  or " "  or something else.
I want to test a textbox where the user didn't even click inside of it.
brothertruffle880Asked:
Who is Participating?
 
Subodh Tiwari (Neeraj)Connect With a Mentor Excel & VBA ExpertCommented:
If the user didn't click that TextBox, that TextBox will be empty while submitting the values, why don't you check it then if the SalaryEarnings is completely empty and the user tries to submit the values?

Otherwise you may try something like this....

1) First declare a boolean variable in the beginning of the UserForm Module like this.....
Dim Clicked As Boolean

2) Then on the UserForm Initialize event, write the following code...
Private Sub UserForm_Initialize()
Clicked = False
End Sub

Open in new window


3) Also Place the following code in the UserForm Module (Assuming SalaryEarnings is the Name of the TextBox)
Private Sub SalaryEarnings_Enter()
Clicked = True
End Sub

Open in new window


4) Then you can use the following code to check both your conditions like this.....
If Not Clicked Then
   MsgBox "Either You ignored the SalaryEarnings TextBox.", vbExclamation
   Exit Sub
ElseIf Val(SalaryEarnings.Value) <= 0 Then
   MsgBox "You have entered an invalid value.", vbExclamation, "Invlaid Input!"
   Exit Sub
End If

Open in new window


OR if you wish, you may check both the condition in a single statement like this...
If Val(SalaryEarnings.Value) <= 0 or Not Clicked Then

Open in new window

0
 
Martin LissOlder than dirtCommented:
Is this a textbox on a userform? Is SalaryEarnings the name of the textbox?
0
 
brothertruffle880Author Commented:
Hi Martin Liss:
Yes.  this a textbox on a userform and SalaryEarnings the name of the textbox
0
 
brothertruffle880Author Commented:
Hi Martin Liss:
 Yes.  this a textbox on a userform and SalaryEarnings the name of the textbox
0
 
Martin LissOlder than dirtCommented:
Is there something wrong with Neeraj's solution? If so, what?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.