PC Review


Reply
Thread Tools Rate Thread

Application.InputBox usage

 
 
ArthurJ
Guest
Posts: n/a
 
      6th Nov 2008
I want the user to input a number. I want to handle the three possible
situations:
1. User enters number
2. User clicks Cancel
3. User enters nothing and clicks OK

I am having trouble with the third possibility, where the user enters nothing.

Below is some of the code I have now:

Sub GetNumber
Dim myNum As Variant
On Error Resume Next 'This line seems to have no effect.
myNum = Application.InputBox(Prompt:="Enter a number", Type:=1)
'If user enters nothing and clicks OK, Excel generates an info box about
'entering an incorrect formula. But it is NOT a VBA error so cannot be
trapped
'with normal methods.

If myNum = False Then
MsgBox ("Cancel was chosen. Macro will end.")
Exit Sub
Else
MsgBox (myNum)
End If
End Sub

Thank you,
Art
 
Reply With Quote
 
 
 
 
Susan
Guest
Posts: n/a
 
      6th Nov 2008
Sub GetNumber
Dim myNum As Variant
On Error Resume Next 'This line seems to have no effect.
myNum = Application.InputBox(Prompt:="Enter a number", Type:=1)

'.............................new
coding................................
if myNum = "" then
msgbox "Please enter a number in the input box."
exit sub
end if
'.............................................................................

'If user enters nothing and clicks OK, Excel generates an info box
about
'entering an incorrect formula. But it is NOT a VBA error so
cannot be
trapped
'with normal methods.


If myNum = False Then
MsgBox ("Cancel was chosen. Macro will end.")
Exit Sub
Else
MsgBox (myNum)
End If
End Sub


try that.

susan


On Nov 6, 10:48*am, ArthurJ <Arth...@discussions.microsoft.com> wrote:
> I want the user to input a number. I want to handle the three possible
> situations:
> 1. User enters number
> 2. User clicks Cancel
> 3. User enters nothing and clicks OK
>
> I am having trouble with the third possibility, where the user enters nothing.
>
> Below is some of the code I have now:
>
> Sub GetNumber
> * * Dim myNum As Variant
> * * On Error Resume Next * *'This line seems to have no effect.
> * * myNum = Application.InputBox(Prompt:="Enter a number", Type:=1)
> * * 'If user enters nothing and clicks OK, Excel generates an info box about
> * * 'entering an incorrect formula. But it is NOT a VBA error so cannot be
> trapped
> * * 'with normal methods.
>
> * * If myNum = False Then
> * * * * MsgBox ("Cancel was chosen. Macro will end.")
> * * * * Exit Sub
> * * Else
> * * * * MsgBox (myNum)
> * * End If
> End Sub
>
> Thank you,
> Art


 
Reply With Quote
 
Susan
Guest
Posts: n/a
 
      6th Nov 2008
nope.......... should have tested it before i posted it. still
triggers that excel error message. tried putting
Application.DisplayAlerts=false in there.... but that didn't help
because it got rid of the excel error message but didn't let the macro
continue.
sorry!
susan


On Nov 6, 11:09*am, Susan <bogenex...@aol.com> wrote:
> Sub GetNumber
> * * Dim myNum As Variant
> * * On Error Resume Next * *'This line seems to have no effect.
> * * myNum = Application.InputBox(Prompt:="Enter a number", Type:=1)
>
> '.............................newcoding................................
> * * if myNum = "" then
> * * * * *msgbox "Please enter a number in the input box."
> * * * * *exit sub
> * * end if
> '...........................................................................*..
>
> * * 'If user enters nothing and clicks OK, Excel generates an info box
> about
> * * 'entering an incorrect formula. But it is NOT a VBA error so
> cannot be
> trapped
> * * 'with normal methods.
>
> * * If myNum = False Then
> * * * * MsgBox ("Cancel was chosen. Macro will end.")
> * * * * Exit Sub
> * * Else
> * * * * MsgBox (myNum)
> * * End If
> End Sub
>
> try that.
>
> susan
>
> On Nov 6, 10:48*am, ArthurJ <Arth...@discussions.microsoft.com> wrote:
>
>
>
> > I want the user to input a number. I want to handle the three possible
> > situations:
> > 1. User enters number
> > 2. User clicks Cancel
> > 3. User enters nothing and clicks OK

>
> > I am having trouble with the third possibility, where the user enters nothing.

>
> > Below is some of the code I have now:

>
> > Sub GetNumber
> > * * Dim myNum As Variant
> > * * On Error Resume Next * *'This line seems to have no effect.
> > * * myNum = Application.InputBox(Prompt:="Enter a number", Type:=1)
> > * * 'If user enters nothing and clicks OK, Excel generates an info box about
> > * * 'entering an incorrect formula. But it is NOT a VBA error so cannot be
> > trapped
> > * * 'with normal methods.

>
> > * * If myNum = False Then
> > * * * * MsgBox ("Cancel was chosen. Macro will end.")
> > * * * * Exit Sub
> > * * Else
> > * * * * MsgBox (myNum)
> > * * End If
> > End Sub

>
> > Thank you,
> > Art- Hide quoted text -

>
> - Show quoted text -


 
Reply With Quote
 
ArthurJ
Guest
Posts: n/a
 
      6th Nov 2008


"Susan" wrote:

> nope.......... should have tested it before i posted it. still
> triggers that excel error message. tried putting
> Application.DisplayAlerts=false in there.... but that didn't help
> because it got rid of the excel error message but didn't let the macro
> continue.
> sorry!


Susan, that's Ok! Same problem that tripped me up. Maybe I need to use one
of the other input boxes, not Application.InputBox.

Art
> susan
>
>
> On Nov 6, 11:09 am, Susan <bogenex...@aol.com> wrote:
> > Sub GetNumber
> > Dim myNum As Variant
> > On Error Resume Next 'This line seems to have no effect.
> > myNum = Application.InputBox(Prompt:="Enter a number", Type:=1)
> >
> > '.............................newcoding................................
> > if myNum = "" then
> > msgbox "Please enter a number in the input box."
> > exit sub
> > end if
> > '...........................................................................Â*..
> >
> > 'If user enters nothing and clicks OK, Excel generates an info box
> > about
> > 'entering an incorrect formula. But it is NOT a VBA error so
> > cannot be
> > trapped
> > 'with normal methods.
> >
> > If myNum = False Then
> > MsgBox ("Cancel was chosen. Macro will end.")
> > Exit Sub
> > Else
> > MsgBox (myNum)
> > End If
> > End Sub
> >
> > try that.
> >
> > susan
> >
> > On Nov 6, 10:48 am, ArthurJ <Arth...@discussions.microsoft.com> wrote:
> >
> >
> >
> > > I want the user to input a number. I want to handle the three possible
> > > situations:
> > > 1. User enters number
> > > 2. User clicks Cancel
> > > 3. User enters nothing and clicks OK

> >
> > > I am having trouble with the third possibility, where the user enters nothing.

> >
> > > Below is some of the code I have now:

> >
> > > Sub GetNumber
> > > Dim myNum As Variant
> > > On Error Resume Next 'This line seems to have no effect.
> > > myNum = Application.InputBox(Prompt:="Enter a number", Type:=1)
> > > 'If user enters nothing and clicks OK, Excel generates an info box about
> > > 'entering an incorrect formula. But it is NOT a VBA error so cannot be
> > > trapped
> > > 'with normal methods.

> >
> > > If myNum = False Then
> > > MsgBox ("Cancel was chosen. Macro will end.")
> > > Exit Sub
> > > Else
> > > MsgBox (myNum)
> > > End If
> > > End Sub

> >
> > > Thank you,
> > > Art- Hide quoted text -

> >
> > - Show quoted text -

>
>

 
Reply With Quote
 
john
Guest
Posts: n/a
 
      6th Nov 2008
this may do what you are looking for:

Sub GetNumber()
Dim myNum As Variant

Application.DisplayAlerts = False

myNum = Application.InputBox(Prompt:="Enter A Number", Title:="Enter A
Number", Type:=1)

If VarType(myNum) = vbBoolean Then
If myNum = False Then
Debug.Print "cancelled"

MsgBox ("Cancel was chosen. Macro will end.")


Else

MsgBox myNum

End If

End If

Application.DisplayAlerts = True

End Sub

--
jb


"ArthurJ" wrote:

> I want the user to input a number. I want to handle the three possible
> situations:
> 1. User enters number
> 2. User clicks Cancel
> 3. User enters nothing and clicks OK
>
> I am having trouble with the third possibility, where the user enters nothing.
>
> Below is some of the code I have now:
>
> Sub GetNumber
> Dim myNum As Variant
> On Error Resume Next 'This line seems to have no effect.
> myNum = Application.InputBox(Prompt:="Enter a number", Type:=1)
> 'If user enters nothing and clicks OK, Excel generates an info box about
> 'entering an incorrect formula. But it is NOT a VBA error so cannot be
> trapped
> 'with normal methods.
>
> If myNum = False Then
> MsgBox ("Cancel was chosen. Macro will end.")
> Exit Sub
> Else
> MsgBox (myNum)
> End If
> End Sub
>
> Thank you,
> Art

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      6th Nov 2008
By using application.inputbox and type:=1, you're telling excel that they have
to enter a number. This means that your code doesn't have to do anything to
check to see if the entry is a number.

If you don't want that warning message, then you can use:

myNum = inputbox(Prompt:="enter a number")

But now all the validation will be your responsibility.



ArthurJ wrote:
>
> I want the user to input a number. I want to handle the three possible
> situations:
> 1. User enters number
> 2. User clicks Cancel
> 3. User enters nothing and clicks OK
>
> I am having trouble with the third possibility, where the user enters nothing.
>
> Below is some of the code I have now:
>
> Sub GetNumber
> Dim myNum As Variant
> On Error Resume Next 'This line seems to have no effect.
> myNum = Application.InputBox(Prompt:="Enter a number", Type:=1)
> 'If user enters nothing and clicks OK, Excel generates an info box about
> 'entering an incorrect formula. But it is NOT a VBA error so cannot be
> trapped
> 'with normal methods.
>
> If myNum = False Then
> MsgBox ("Cancel was chosen. Macro will end.")
> Exit Sub
> Else
> MsgBox (myNum)
> End If
> End Sub
>
> Thank you,
> Art


--

Dave Peterson
 
Reply With Quote
 
john
Guest
Posts: n/a
 
      6th Nov 2008
typed it too fast! Put the MsgBox myNum in wrong place!!

Sub GetNumber()
Dim myNum As Variant

Application.DisplayAlerts = False

myNum = Application.InputBox(Prompt:="Enter A Number", Title:="Enter A
Number", Type:=1)

If VarType(myNum) = vbBoolean Then

If myNum = False Then
Debug.Print "cancelled"

MsgBox ("Cancel was chosen. Macro will end.")


End If

Else

MsgBox myNum

End If



Application.DisplayAlerts = True

End Sub
--
jb


"john" wrote:

> this may do what you are looking for:
>
> Sub GetNumber()
> Dim myNum As Variant
>
> Application.DisplayAlerts = False
>
> myNum = Application.InputBox(Prompt:="Enter A Number", Title:="Enter A
> Number", Type:=1)
>
> If VarType(myNum) = vbBoolean Then
> If myNum = False Then
> Debug.Print "cancelled"
>
> MsgBox ("Cancel was chosen. Macro will end.")
>
>
> Else
>
> MsgBox myNum
>
> End If
>
> End If
>
> Application.DisplayAlerts = True
>
> End Sub
>
> --
> jb
>
>
> "ArthurJ" wrote:
>
> > I want the user to input a number. I want to handle the three possible
> > situations:
> > 1. User enters number
> > 2. User clicks Cancel
> > 3. User enters nothing and clicks OK
> >
> > I am having trouble with the third possibility, where the user enters nothing.
> >
> > Below is some of the code I have now:
> >
> > Sub GetNumber
> > Dim myNum As Variant
> > On Error Resume Next 'This line seems to have no effect.
> > myNum = Application.InputBox(Prompt:="Enter a number", Type:=1)
> > 'If user enters nothing and clicks OK, Excel generates an info box about
> > 'entering an incorrect formula. But it is NOT a VBA error so cannot be
> > trapped
> > 'with normal methods.
> >
> > If myNum = False Then
> > MsgBox ("Cancel was chosen. Macro will end.")
> > Exit Sub
> > Else
> > MsgBox (myNum)
> > End If
> > End Sub
> >
> > Thank you,
> > Art

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Application.InputBox(,,,,,8) JsJ_Slim Microsoft Excel Programming 3 8th Aug 2008 02:32 AM
Application.InputBox =?Utf-8?B?U3RldmVu?= Microsoft Excel Programming 2 23rd Dec 2006 11:56 PM
application.inputbox =?Utf-8?B?Q2hyaXN0bWFzIE1heQ==?= Microsoft Excel Programming 5 22nd Nov 2006 04:11 PM
Inputbox and Application.InputBox Maria Microsoft Excel Programming 1 20th Sep 2004 11:36 AM
application.inputbox Murat Microsoft Excel Programming 4 24th Feb 2004 11:38 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:36 PM.