PDA

View Full Version : Solved: input box loop



rob0923
08-19-2009, 05:41 PM
Hi,

I am inserting an input box into the program I am writing, which will be a number value.

I have the input box working correctly, but I want to make a loop so if nothing is enetered it will pop up a new input box stating that something has to be enetered and only a numbered value can work. I beleive I have to enter "type 8" in for numerical value. If they choose not to continue with and press cancel I would like it to close exit the sub, but I keep getting an error when I press cancel.

Thanks in advance for any input

GTO
08-19-2009, 06:23 PM
Greetings,

As you indicated type, you are using the Application.InputBox Method. You'll want type 1 to insist on a numerical value.

As this will return a zero if they cancel, you could toss a test in after, to see if the user wants another shot at glory or to chicken out. Maybe:


Option Explicit

Sub exa()
Dim dblRetVal As Double

Do While dblRetVal = 0
dblRetVal = Application.InputBox("Enter a number", "", , , , , , 1)
If dblRetVal = 0 Then
If Not MsgBox("You must enter a valid number to continue. Click on the" & _
vbCrLf & _
"<Yes> button to try again, or the <No> button to quit.", _
vbYesNo + vbDefaultButton1 + vbExclamation, vbNullString) = _
vbYes Then

Exit Do '<--- or Exit Sub to bail out.
End If
End If
Loop

MsgBox dblRetVal
End Sub



Hope that helps,

Mark

mikerickson
08-19-2009, 08:25 PM
Perhaps something like this:
Dim promptPrefix
Dim uiValue As Variant
promptPrefix = vbNullString

Do
uiValue = Application.InputBox(promptPrefix & "Enter Something", Type:=3)
promptPrefix = "Numeric Entry Required." & vbCr
If uiValue = False Then Exit Sub
Loop Until IsNumeric(uiValue)

MsgBox uiValue

GTO
08-19-2009, 10:46 PM
@mickerickson:

Howdy Mike,

Say, what's a Type:=3? It certainly works fine; just curious for my education. (Its not listed in Help)

Thank you so much,

Mark

mdmackillop
08-20-2009, 12:46 AM
Hi Mark,
From the Help file
Remarks

The following table lists the values that can be passed in the Type argument. Can be one or a sum of the values. For example, for an input box that can accept both text and numbers, set Type to 1 + 2.
Value Meaning
0 A formula
1 A number
2 Text (a string)
4 A logical value (True or False)
8 A cell reference, as a Range object
16 An error value, such as #N/A
64 An array of values

GTO
08-20-2009, 01:05 AM
Hi Mark,
From the Help file
Remarks

The following table lists the values that can be passed in the Type argument. Can be one or a sum of the values. For example, for an input box that can accept both text and numbers, set Type to 1 + 2.
Value Meaning
0 A formula
1 A number
2 Text (a string)
4 A logical value (True or False)
8 A cell reference, as a Range object
16 An error value, such as #N/A
64 An array of values



(Its not listed in Help)
footinmout

As always, thank you Malcom. I will go find myself a dunce cap to wear until I learn to read .:hide:

Mark

rob0923
08-20-2009, 05:43 AM
Thanks! If only I had an extensive knowledge as everyone else at this! :yes