Error Handling on InputBoxes

cdhoughton

Board Regular
Joined
Dec 5, 2003
Messages
170
What kind of error handling procedures are available on inputboxes? Principally I need the following validation:

1) The user must enter an integer value
2) The user must enter a percentage value
3) The user cannot press cancel or leave a blank entry

Any pointers on the above problems?

Thanks in advance for any advice!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You can do something like

Dim x as Integer

on error resume next
x=Inputbox("Input a number")
if err <> = then
msgbox "Error"
else
end if
on error goto 0

Of course you can do whatever you want to handle the error and not just a message box.

For percentage you could have x be a single (Dim x as Single) and do the same thing.

For blank entry you can try

If len(x)=0 then etc...
 
Upvote 0
If you get stuck post back with what you specifically want to happen if the wrong data is inputted.

Take Care
 
Upvote 0
Hi Jacob,

It's not letting me have:

If err <> = Then

It's complaining of expecting an expression. Can you help?
 
Upvote 0
Hi,

If err <> = Then should be

If err <> 0 Then

An alternative is to use the inputbox function supplied by Excel. It has several advantages over the VBA inputbox:-

<ul>
[*]You can specify what sort of data you want to allow e.g. numbers, text, a range.
[*]It allows you to specifically capture the user pressing cancel, something that the inputbox doesn't do very well.

Here's some sample code:-

Code:
Sub EnterNumber()
    Dim vRetval As Variant

    vRetval = Application.InputBox("Enter a number", "Inputbox Demo")

    If vRetval = False Then Exit Sub    'Cancel was pressed

    If vRetval - Int(vRetval) <> 0 Then

        MsgBox "A non-integer number was entered"

    Else

        MsgBox "You entered an integer"

    End If


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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