ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   What type is vbCancel? (https://www.excelbanter.com/excel-programming/291726-what-type-vbcancel.html)

Shatin

What type is vbCancel?
 
In the code fragment below, when I push the cancel button in response to the
inputbox, I get a type mismatch error. What type is vbCancel?

Dim myDate as String

Do
myDate = InputBox("Please enter your name.")
If myDate = vbCancel Then
Exit Sub
ElseIf myDate = "" Then
MsgBox ("You didn't enter anything. Please try again.")
End If
Loop While myDate = ""

In the same macro, I have these similar lines which work OK:

Dim myCheck as String

mycheck = MsgBox("Proceed?", vbYesNo)
If myCheck = vbNo Then
Exit Sub
End If

Why don't I get any type mismatch error with vbNo?



Bob Phillips[_6_]

What type is vbCancel?
 
Hi Shatin,

It's an integer. As you want a date, declare it as variant to handle both
types.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Shatin" wrote in message
...
In the code fragment below, when I push the cancel button in response to

the
inputbox, I get a type mismatch error. What type is vbCancel?

Dim myDate as String

Do
myDate = InputBox("Please enter your name.")
If myDate = vbCancel Then
Exit Sub
ElseIf myDate = "" Then
MsgBox ("You didn't enter anything. Please try again.")
End If
Loop While myDate = ""

In the same macro, I have these similar lines which work OK:

Dim myCheck as String

mycheck = MsgBox("Proceed?", vbYesNo)
If myCheck = vbNo Then
Exit Sub
End If

Why don't I get any type mismatch error with vbNo?





Steven Fatzinger

What type is vbCancel?
 
Shatin wrote:
In the code fragment below, when I push the cancel button in response to the
inputbox, I get a type mismatch error. What type is vbCancel?

Dim myDate as String

Do
myDate = InputBox("Please enter your name.")
If myDate = vbCancel Then
Exit Sub
ElseIf myDate = "" Then
MsgBox ("You didn't enter anything. Please try again.")
End If
Loop While myDate = ""

In the same macro, I have these similar lines which work OK:

Dim myCheck as String

mycheck = MsgBox("Proceed?", vbYesNo)
If myCheck = vbNo Then
Exit Sub
End If

Why don't I get any type mismatch error with vbNo?


Believe it or not, you should always check the help file first.
The InputBox returns a String, MsgBox returns an Integer.
Both vbNo and vbCancel are Integers.
Your InputBox line places a string in myDate.
You then compare that string to vbNo, which is an Integer
It would be better to use the following test:
If Len(myDate)=0 Then Exit Sub

Helen Trim[_4_]

What type is vbCancel?
 

vbCancel is an integer, but that's a red herring here.
You get an integer returned from MsgBox if you use it with
buttons, and that is what vbCancel is for.

For an inputbox, if you press the Cancel button, it reurns
an empty string. So you only need the test for empty
string, which you already have.

Do
myDate = InputBox("Please enter your name.")
If myDate = "" Then
MsgBox ("You didn't enter anything. Please try
again.")
End If
Loop While myDate = ""

HTH
Helen


-----Original Message-----
In the code fragment below, when I push the cancel button

in response to the
inputbox, I get a type mismatch error. What type is

vbCancel?

Dim myDate as String

Do
myDate = InputBox("Please enter your name.")
If myDate = vbCancel Then
Exit Sub
ElseIf myDate = "" Then
MsgBox ("You didn't enter anything. Please try

again.")
End If
Loop While myDate = ""

In the same macro, I have these similar lines which work

OK:

Dim myCheck as String

mycheck = MsgBox("Proceed?", vbYesNo)
If myCheck = vbNo Then
Exit Sub
End If

Why don't I get any type mismatch error with vbNo?


.


Chip Pearson

What type is vbCancel?
 
Just for the record, vbCancel and all other results of a MsgBox
are Longs, not Integers.


"Steven Fatzinger" wrote in message
...
Shatin wrote:
In the code fragment below, when I push the cancel button in

response to the
inputbox, I get a type mismatch error. What type is vbCancel?

Dim myDate as String

Do
myDate = InputBox("Please enter your name.")
If myDate = vbCancel Then
Exit Sub
ElseIf myDate = "" Then
MsgBox ("You didn't enter anything. Please try

again.")
End If
Loop While myDate = ""

In the same macro, I have these similar lines which work OK:

Dim myCheck as String

mycheck = MsgBox("Proceed?", vbYesNo)
If myCheck = vbNo Then
Exit Sub
End If

Why don't I get any type mismatch error with vbNo?


Believe it or not, you should always check the help file first.
The InputBox returns a String, MsgBox returns an Integer.
Both vbNo and vbCancel are Integers.
Your InputBox line places a string in myDate.
You then compare that string to vbNo, which is an Integer
It would be better to use the following test:
If Len(myDate)=0 Then Exit Sub




Steven Fatzinger

What type is vbCancel?
 
Chip Pearson wrote:

Just for the record, vbCancel and all other results of a MsgBox
are Longs, not Integers.


"Steven Fatzinger" wrote in message
...

Shatin wrote:

In the code fragment below, when I push the cancel button in


response to the

inputbox, I get a type mismatch error. What type is vbCancel?

Dim myDate as String

Do
myDate = InputBox("Please enter your name.")
If myDate = vbCancel Then
Exit Sub
ElseIf myDate = "" Then
MsgBox ("You didn't enter anything. Please try


again.")

End If
Loop While myDate = ""

In the same macro, I have these similar lines which work OK:

Dim myCheck as String

mycheck = MsgBox("Proceed?", vbYesNo)
If myCheck = vbNo Then
Exit Sub
End If

Why don't I get any type mismatch error with vbNo?



Believe it or not, you should always check the help file first.
The InputBox returns a String, MsgBox returns an Integer.
Both vbNo and vbCancel are Integers.
Your InputBox line places a string in myDate.
You then compare that string to vbNo, which is an Integer
It would be better to use the following test:
If Len(myDate)=0 Then Exit Sub




Technically, you are correct for 32 bit systems.
However, Help still states that it returns an Integer.
Plus, I didn't want to confuse the issue by introducing the Long versus
Integer discussion.


All times are GMT +1. The time now is 06:12 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com