Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,814
Default boolean variable?

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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default boolean variable?

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" skrev i meddelelsen
...
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.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default boolean variable?

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" wrote in message
...
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.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default boolean variable?

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" wrote in message
...
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" skrev i meddelelsen
...
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.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default boolean variable?

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


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default boolean variable?

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" skrev i meddelelsen
...
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
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 131
Default boolean variable?

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.

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default boolean variable?

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" wrote in message
...
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" skrev i meddelelsen
...
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


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,814
Default boolean variable?

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!

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,814
Default boolean variable?

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" skrev i meddelelsen
...
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.





  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default boolean variable?

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.



Rick Rothstein wrote:

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" wrote in message
...
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" skrev i meddelelsen
...
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


--

Dave Peterson
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default boolean variable?

Dim myVar as boolean
dim Answer as long

Steve wrote:

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" skrev i meddelelsen
...
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
  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,814
Default boolean variable?

Ah...... got it! Thank you Dave. (As I flat palm my forehead....... )

"Dave Peterson" wrote:

Dim myVar as boolean
dim Answer as long

Steve wrote:

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" skrev i meddelelsen
...
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

  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 947
Default boolean variable?

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



On 10/1/09 2:16 PM, 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
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
Set Boolean Variable for Changing Any Control on Userform Ryan H Excel Programming 3 July 10th 09 05:28 PM
Boolean Variable Definition James Excel Programming 1 August 14th 08 06:42 PM
More on Boolean Epinn New Users to Excel 7 November 28th 06 09:29 AM
VBA Boolean Jeff Excel Discussion (Misc queries) 1 February 2nd 06 10:01 PM
UserForm not passing Boolean variable Chip Pearson Excel Programming 2 July 19th 03 06:06 PM


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

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"