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?
 
This seems to work;
Code:
Private Sub CommandButton1_Click()
Select Case TextBox1.Value
Case 1 To 8
GoTo 2
Case Is > 8
MsgBox "TextBox1 is too High"
Exit Sub
End Select
2
Select Case TextBox2.Value
Case 1 To 8
MsgBox "Run your Code"
Case Is > 8
MsgBox "TextBox2 is too High"
End Select
End Sub

HTH
Colin
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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