inputbox accepting only number

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I want to improve the code below. If user will enter a text instead of number the program will not stop because of "type miss match" message but maybe give a warning message to user or something. How can I do that? Thank you so much.
Code:
Sub studentmarks()
'to find students grade
    Dim x As Integer
    Dim z As Integer
    For z = 1 To 5
        x = InputBox("number")
        If x > 90 Then
            MsgBox "a"
        ElseIf x > 70 Then
            MsgBox "b"
        Else
            MsgBox "f"
        End If
    Next z
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
What do you want to happen if Cancel is selected?
 
Upvote 0
Try using Application.InputBox with a Type argument of 1 instead of just InputBox. Execute this in the Immediate Window and see what happens if a non-number is entered...

MsgBox Application.InputBox("Enter a number please", Type:=1)

You can see all the rest of the possible arguments by putting the text cursor next to the word InputBox in the command Application.InputBox and pressing the F1 key.
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,632
Members
452,661
Latest member
Nonhle

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