Parentheses to evaluate True/False?

cbrf23

Board Regular
Joined
Jun 20, 2011
Messages
241
I'm trying to create a dynamic user form through vba. Since I'm not real familiar with forms, I downloaded an example to kind of reverse engineer. When going through the code, I noticed a line using a syntax I'm not familiar with:
Code:
CmdBttnDE.Enabled = (SpinButton1.Value > 0)

This code disables the button if the spinbutton1 value is not greater than 0. I didn't know you could do that...pretty cool trick. Seems more efficient then:
Code:
if not SpinButton1.Value > 0 then CmdBttnDE.Enabled = False

So, I'm wondering how exactly this syntax works. Does throwing parenthes around a statment like that automatically ask vba to evaluate the statment to true or false? I tested and the same line works with the parentheses removed... Anyone have any insight on this?
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
The parentheses are there in order to make the order of evaluation clearer. You could also write
Code:
CmdBttnDE.Enabled = SpinButton1.Value = 0
Here, the rightmost equal sign is being used as comparison operator while the leftmost equal sign is being used as an assignment operator. The comparison has to be done first and then the result of the comparison gets assigned to the variable. But writing it as
Code:
CmdBttnDE.Enabled = (SpinButton1.Value = 0)
makes it clearer. And it would be necessary in a case such as
Code:
 X = Y = Z = W < V
in this satement X would end up being assigned either True or False depending on how the chain of evaluations of the variables to the right worked out. But I for one am not sure what the order would be - I would have to look it up because the order always seems to depend on whatever language you are working in. It's certainly clearer (for both the author and any subsequent reader) if the author imposes the order of operations using parentheses.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top