Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 266
Default Avoiding error on Input Box - if no value is entered

I have a line like this:

MyData = InputBox(Prompt:="Enter Data", Title:="ENTER Data")

If the user selects "Cancel", I get a Run-time error 13 - Type Mismatch.
How can I avoid this error when the user selects cancel ? Thanks for any
help on this.
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Avoiding error on Input Box - if no value is entered

InputBox will return 0 if Cancel is pressed.

Make sure you have Dim'ed MyData so as to accept 0
--
Gary''s Student - gsnu200849


"dhstein" wrote:

I have a line like this:

MyData = InputBox(Prompt:="Enter Data", Title:="ENTER Data")

If the user selects "Cancel", I get a Run-time error 13 - Type Mismatch.
How can I avoid this error when the user selects cancel ? Thanks for any
help on this.

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,520
Default Avoiding error on Input Box - if no value is entered

Check the result and Exit Sub

MyData = InputBox(Prompt:="Enter Data", Title:="ENTER Data")
If Trim(MyData) = "" Then Exit Sub

OR

If you want to force the user to enter something then keep this within a loop

Do
MyData = InputBox(Prompt:="Enter Data", Title:="ENTER Data")
Loop Until Trim(MyData) < ""


If this post helps click Yes
---------------
Jacob Skaria


"dhstein" wrote:

I have a line like this:

MyData = InputBox(Prompt:="Enter Data", Title:="ENTER Data")

If the user selects "Cancel", I get a Run-time error 13 - Type Mismatch.
How can I avoid this error when the user selects cancel ? Thanks for any
help on this.

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Avoiding error on Input Box - if no value is entered

Sorry about the first post, ignore it. Try:

Sub qwerty()
x = InputBox(prompt:="feed me")
MsgBox (x)
x = Application.InputBox(prompt:="feed me")
MsgBox (x)
End Sub

The first call will return a null string.
The second call will retrun FALSE.

So MyData must be Dim'ed to handle the null string.
--
Gary''s Student - gsnu200849


"dhstein" wrote:

I have a line like this:

MyData = InputBox(Prompt:="Enter Data", Title:="ENTER Data")

If the user selects "Cancel", I get a Run-time error 13 - Type Mismatch.
How can I avoid this error when the user selects cancel ? Thanks for any
help on this.

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,520
Default Avoiding error on Input Box - if no value is entered

Oops..If that is a type mismatch check your declaration and adjust the code
as below to handle blanks

Dim mydata As Integer
mydata = CInt("0" & InputBox(Prompt:="Enter Data", Title:="ENTER Data"))


If this post helps click Yes
---------------
Jacob Skaria


"Jacob Skaria" wrote:

Check the result and Exit Sub

MyData = InputBox(Prompt:="Enter Data", Title:="ENTER Data")
If Trim(MyData) = "" Then Exit Sub

OR

If you want to force the user to enter something then keep this within a loop

Do
MyData = InputBox(Prompt:="Enter Data", Title:="ENTER Data")
Loop Until Trim(MyData) < ""


If this post helps click Yes
---------------
Jacob Skaria


"dhstein" wrote:

I have a line like this:

MyData = InputBox(Prompt:="Enter Data", Title:="ENTER Data")

If the user selects "Cancel", I get a Run-time error 13 - Type Mismatch.
How can I avoid this error when the user selects cancel ? Thanks for any
help on this.



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 266
Default Avoiding error on Input Box - if no value is entered

Thanks for all the responses. I used Gary's method, Dim'ed the variable as
Variant and it's working.



"Gary''s Student" wrote:

Sorry about the first post, ignore it. Try:

Sub qwerty()
x = InputBox(prompt:="feed me")
MsgBox (x)
x = Application.InputBox(prompt:="feed me")
MsgBox (x)
End Sub

The first call will return a null string.
The second call will retrun FALSE.

So MyData must be Dim'ed to handle the null string.
--
Gary''s Student - gsnu200849


"dhstein" wrote:

I have a line like this:

MyData = InputBox(Prompt:="Enter Data", Title:="ENTER Data")

If the user selects "Cancel", I get a Run-time error 13 - Type Mismatch.
How can I avoid this error when the user selects cancel ? Thanks for any
help on this.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code to Query SQL Server with a parameter value (entered into an Input Box), and have that value also display in a selected cell on a worksheet Doctorjones_md Excel Discussion (Misc queries) 3 June 27th 07 04:14 PM
Code to Query SQL Server with a parameter value (entered into an Input Box), and have that value also display in a selected cell on a worksheet Doctorjones_md Excel Worksheet Functions 3 June 27th 07 04:14 PM
Avoiding Value error message due to space in cell w/ formula A.R. Hunt Excel Discussion (Misc queries) 3 January 26th 07 09:52 PM
Avoiding the dreaded #div/0 error Marie1uk Excel Worksheet Functions 10 July 21st 05 10:49 PM
Input Date when data is entered into another cell GaryByrd Excel Worksheet Functions 2 February 1st 05 12:37 AM


All times are GMT +1. The time now is 07:17 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"