PC Review


Reply
Thread Tools Rate Thread

boolean variable?

 
 
Steve
Guest
Posts: n/a
 
      1st Oct 2009
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.
 
Reply With Quote
 
 
 
 
Per Jessen
Guest
Posts: n/a
 
      1st Oct 2009
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

"Steve" <(E-Mail Removed)> skrev i meddelelsen
news:5B319A31-797F-4B99-940E-(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      1st Oct 2009
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.

--
Rick (MVP - Excel)


"Steve" <(E-Mail Removed)> wrote in message
news:5B319A31-797F-4B99-940E-(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      1st Oct 2009
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.

--
Rick (MVP - Excel)


"Per Jessen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
> "Steve" <(E-Mail Removed)> skrev i meddelelsen
> news:5B319A31-797F-4B99-940E-(E-Mail Removed)...
>> 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.

>


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      1st Oct 2009
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






Steve wrote:
>
> 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.


--

Dave Peterson
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      1st Oct 2009
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

Per Jessen wrote:
>
> 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
>
> "Steve" <(E-Mail Removed)> skrev i meddelelsen
> news:5B319A31-797F-4B99-940E-(E-Mail Removed)...
> > 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.


--

Dave Peterson
 
Reply With Quote
 
B Lynn B
Guest
Posts: n/a
 
      1st Oct 2009
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.

"Steve" wrote:

> 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.

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      1st Oct 2009
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

--
Rick (MVP - Excel)


"Dave Peterson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
> Per Jessen wrote:
>>
>> 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
>>
>> "Steve" <(E-Mail Removed)> skrev i meddelelsen
>> news:5B319A31-797F-4B99-940E-(E-Mail Removed)...
>> > 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.

>
> --
>
> Dave Peterson


 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      1st Oct 2009
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!

 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      1st Oct 2009
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.

"Per Jessen" wrote:

> 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
>
> "Steve" <(E-Mail Removed)> skrev i meddelelsen
> news:5B319A31-797F-4B99-940E-(E-Mail Removed)...
> > 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.

>
>

 
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
Boolean Variable Definition James Microsoft Excel Programming 1 14th Aug 2008 06:42 PM
When and how to set a boolean variable to true? Curious Microsoft Dot NET 1 5th Aug 2008 09:20 AM
3-way property of boolean variable lulu Microsoft Access Database Table Design 6 7th Feb 2006 05:04 PM
boolean variable Daniel Groh Microsoft C# .NET 2 19th May 2005 11:11 AM
Boolean variable in a module..Is it possible ? Don Garry Microsoft Access Getting Started 3 5th Aug 2004 05:32 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:30 AM.