boolean variable?

S

Steve

Howdee all.
I have a macro that I've got a variable that presently requires either a
true, or false input.
This macro calls to another function, which only requires an input if the
element is true.
I'd like it to only require an input if the statement is true.

I just tried-

MyVar = Application.InputBox(Prompt:="Is this a font color?- True or False",
Type:=4)

If MyVar= "" Then
MyVar = False
Else: MyVar = True
End If

I receive a type mismatch error when I do this.
 
P

Per Jessen

Hi Steve

I would not rely on the user to enter True or False, just use a msgbox to
answer yes or no, and then turn the answer to at boolean value:

Dim MyVar As Boolean
Answer = MsgBox("Is this a font color?", vbYesNo + vbQuestion, "True or
False")
If Answer = vbYes Then MyVar = True
'A boolean varieble is false by default

Regards,
Per
 
R

Rick Rothstein

It looks like Type:=4 return the text string "Boolean" if the Cancel button
is pressed, so you should be able to do your code this way...

MyVar = Application.InputBox(Prompt:= _
"Is this a font color?- True or False", Type:=4)
If MyVar = Boolean Then MyVar = False

You definitely do NOT want the Else condition you showed as that would
change a legitimately entered False answer to True.
 
R

Rick Rothstein

Steve,

While I gave you the answer you were after in my direct reply to your
original message, I want to fully endorse Per's suggested approach...
without question, it would be the best way for you to proceed.
 
D

Dave Peterson

How about just making sure that the variable is declared as a boolean:

Dim myVar As Boolean
myVar = Application.InputBox(Prompt:="Is this a font color?- True or False", _
Type:=4)
msgbox myVar
 
D

Dave Peterson

Or drop the boolean completely:

Dim Answer as Long
Answer = MsgBox(Prompt:="Is this a font color?", _
buttons:=vbYesNo + vbQuestion, _
title:="Yes or No")

If Answer = vbYes Then
'do something
else
'do something else
end if
 
B

B Lynn B

Without being able to see the rest of your procedure, my guess would be
you've Dim'd myVar as Boolean, which means it can't hold a string, which is
what your inputbox returns. You would need a different variable to hold the
string and then test its value to see if you want to set myVar to True or
False.
 
R

Rick Rothstein

Or drop the variable completely...

If vbYes = MsgBox(Prompt:="Is this a font color?", Buttons:=vbYesNo + _
vbQuestion, Title:="Yes or No") Then
'do something
Else
'do something else
End If
 
S

Steve

Wow... you guys are "johnny on the spot...."

Thank you, all of you, for your answers.
I like Per's the best. It goes with the general overall idea that I wanted
to accomplish.

Again-- thank you!
 
S

Steve

Hi Per,
Got a question on this usage of yours.

It's defaulting to a false all the time for my boolean variable-- even when
I choose true/yes.
dim MyVar, Answer as boolean


Answer = MsgBox("Is this a font color?", vbYesNo + vbQuestion, "True or
False")
If Answer = vbYes Then MyVar = True
'A boolean variable is false by default

What would cause this?

Thank you.
 
D

Dave Peterson

Sometimes, it's nice to save the response into its own (non-generic) variable.

Then you can test it in multiple locations in the code.
 
D

Dave Peterson

Dim myVar as boolean
dim Answer as long
Hi Per,
Got a question on this usage of yours.

It's defaulting to a false all the time for my boolean variable-- even when
I choose true/yes.
dim MyVar, Answer as boolean

Answer = MsgBox("Is this a font color?", vbYesNo + vbQuestion, "True or
False")
If Answer = vbYes Then MyVar = True
'A boolean variable is false by default

What would cause this?

Thank you.
 
D

Dana DeLouis

MyVar = False
Else: MyVar = True

Just an alternative to If..Then..Else

Dim MyVar As Boolean
Dim Answer As Long

Answer = MsgBox("Is this a font color?", vbYesNo + vbQuestion, "True or
False")

MyVar = (Answer = vbYes)

= = = = = =
Dana DeLouis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top