![]() |
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. |
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. |
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. |
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. |
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 |
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 |
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. |
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 |
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! |
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. |
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 |
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 |
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 |
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. |
All times are GMT +1. The time now is 12:21 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com