VBA user input error handling

rharn

Board Regular
Joined
Jun 21, 2011
Messages
54
I have a userform that requires the user to input an integer between the range of 0-8 for the sub to function correctly. My dilemma is right now I have specified an if/then statement that shows a message box informing the user if they are out of bounds, however after the user clicks ok on the msgbox, the sub skips to the end, and unloads my userform. Is there anyway to inform the user that they are out of range and return to the userform for them to correct their input value?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
The program itself is over 1000 lines long haha...I was hoping for a simple 1 liner that just tells my sub to return to the userform instead of executing the rest of its functions.
 
Upvote 0
Yes but I'm assuming you would like us to use the same variable names in our sample of code as you're using.

Let's see just the code around the MsgBox statement?
 
Last edited:
Upvote 0
What about checking if it is a number

something like:
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Var1()<br><SPAN style="color:#00007F">Dim</SPAN> varInput <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>varInput = InputBox("Enter a number")<br><SPAN style="color:#00007F">If</SPAN> IsNumeric(varInput) <SPAN style="color:#00007F">Then</SPAN><br><SPAN style="color:#007F00">'do what you want</SPAN><br><SPAN style="color:#00007F">Else</SPAN><br>MsgBox "Its not a number"<br><SPAN style="color:#007F00">'then go to the field on the form</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
The basic gist of my code looks like the following:

Code:
    Dim TextBox1 As Long, TextBox2 As Long
 
    TextBox1 = ILsearch.TextBox1.Value
    TextBox2 = ILsearch.TextBox2.Value
 
If (TextBox1 And TextBox2 <= 8) And (TextBox1 And TextBox2 > 0) Then
'
'executes desired sub
'
Else: MsgBox ("Database only includes ionic liquids with a maximum of 8 carbons in cation")
 
End if

So when I enter a value greater than 8 my msgbox pops up as follows but after I click ok it continues on with the sub and unloads my userform and activates another worksheet. I can include more code if necessary also. Thanks.
 
Upvote 0
What triggers this code? A command button?
 
Upvote 0
Could you not just:
Code:
Else
   MsgBox ("Database only includes ionic liquids with a maximum of 8 carbons in cation")
   Exit Sub
End if
 
Upvote 0
My command button code looks like this

Code:
Private Sub CommandButton1_Click()
    PropertySearch.Search
    ActiveSheet.Name = "SearchResult"
    Cells(1, 1).Select
    Unload ILsearch
End Sub

And my purpose is to avoid exiting the sub, if they have values entered into the userform I want to preserve it but inform them that one of the values they have entered is unacceptable.
 
Upvote 0
Then you need to return something from the called routine to indicate that you should not unload. You could convert it to a function that returns True/False depending on whether it completes and then check that in the form code, or pass a Boolean argument ByRef that the sub can change; or if you are feeling lazy, just set a public variable in the form that can be checked before closing.
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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