@ brucemc777
Hi brucemc777,
I too found shg’s post an unusually full and informative reply, and had already mentioned that to shg via Rep comment. I too find it great to know why, rather than just getting it done. Both as you say, - then one can reason better in future, - and of course contribute later better oneself to the Forum.
I just recently started using IIF. I found a good way of remembering what it is about is to make the comparison with the Excel Spreadsheet Function IF
Spreadsheet
Value of this cell in which the IF Formula is in = IF (Logic_Test, Value_if_True, Value_if_False)
VBA
Value of this variable = IIF (Logic_Test, Value_if_True, Value_if_False)
It is not a perfect comparison. For example in a spreadsheet you can use two arguments like these:
And it Gives you this:
_ . I do not think you can get Boolean stuff out of the VBA IIF, or at least you need all three arguments or it errors with “arguments not optional” if you try less than 3 arguments
_ . But the comparison with either a cell or a variable on the LHS of the equation as it were , helps me to keep on track with what the single line VBA IIF is about. VBA IF is a bit different, which i think belongs to the multi line code world of IF Else Nested IFs, Looping etc. etc.But you can ( if you feel so strangely inclined ) usually reduce a set of complicated looping, Nested IFs etc with a single line of Nested IIFs – see here a crazy example:
This code
http://www.excelforum.com/developmen...ml#post4213978
is converted to a single line of IIFs in the first code here
http://www.excelforum.com/developmen...ml#post4213980
Hope that contributes as an alternative “Layman” explanation
Alan
P.s. I think you have the second and third arguments wrong here
IIF (Value > 10, Perform this action if Value is <= 10, Perform this action is Value is > 10)
P. P.s. Maybe if you have any comments on my point/ question 2) below it would be helpful..
_ ...........................................
@shg
Hi shg
You gave a great explanation of the use of parens and a nice explanation of the ByVal (copy of variable) and ByRef (referring specifically to the only copy as it were). That really helped me get that straight. And the extra “nested” parens to “convert” as it were to ByVal ( by virtue of the extra parens and evaluate thing ) was new and i had not read that anywhere.
A couple of quick follow up Comments/questions if i may,
_1) If i understand correctly this is what would be referred to as the “Procedure’s signature“
So in this case defining here arg1 as ByVal and arg2 as ByRef for the Sub Procedure sigshg
That being the case, then going back to your examples, in all the following 4 cases:
I am passing arguments according to the procedure's signature. Is that correct? I ask as your initial explanation seemed to be saying that this was the case for the last 2 only
_2) Although I learned a great deal from your explanation, the relevance to the OP’s question is lost on me. Was his use of the word “Evaluate” in post #3 that which set you off explaining the extra parens and evaluate thing. And hence (1 = 1, 0, 1) is meaningless. That appears only vaguely parallel to why this does not work
These are the correct syntax ( they “work” )
In addition Either of these two seems to work
Or something like this works
So are you saying the parens will try to evaluate if it can. It will recognise a string or something like vbExclamation or something like a Property which when applied to an Object returns a string. It will not get us, for example, to the message box title. ( or give us the Exclamation in the message box, but just its “Evaluated number” 16, (WETF that means!!)
I am following your general idea that VBA uses its Paren pair for different things differently. Which confuses the issue. For example, One might of reasonably expected this to be syntaxly correct,
This form is typically correct when arguments are not named ( and must consequently be given in correct defined order ). But for the Message box it don’t work. - VBA just to confuse us does not use the usual syntax here and omits the paren pair
This code
Can be explained i think along your “extra parens is evaluate” argument. Maybe
Can be thought of as the Explicit version of the Implicit
_ .. just as 2 / 1 + 1 is the Implicit way ( based on the Maths rules of order of evaluating) of writing the explicit ( ( 2 / 1 ) + 1) )
_......
Coming back to the OP.... The VBA IIF is more like the spreadsheet IF, as i tried to explain at the start of this post. Your arguments maybe more relevant to the VBA IF Then , then.
Any comments ? And if not, thanks again for some very informative info, even if possibly not so relevant to the OP question, directly..
Thanks again
Alan
Bookmarks