1

I am writing a VBA code which looks as follows:

If function_one Or Function_two Or Function_three Then
    ' Do Something
End If

So, if any one of the functions return true value, it will do something.

I thought, I have written a small, and efficient code.

But during debugging I found, VBA runs function_one, gets its return value, then runs function_two, gets its return value, and so on for third and then evaluates if condition and proceeds further.

What I want to achieve is, as soon as first function returns true it should do something, and should not execute other functions. If first function fails then only next function should be called.

Later on, I used loop keywords to achieve this task, but it doesn't looks simple/smart. So do you know, how I could write, same code in more optimized way.

Community
  • 1
  • 1
msinfo
  • 1,147
  • 6
  • 21
  • 39
  • VBA is a bit weird when it comes to evaluating statements. How many functions are you calling in this way? I have a hunch there are other ways of optimizing this. – SBI Jun 11 '13 at 06:11
  • See also http://stackoverflow.com/questions/7015471/does-the-vba-and-operator-evaluate-the-second-argument-when-the-first-is-false – John Carter Jun 11 '13 at 06:19
  • @therefromhere Just beat ya with the answer from that question. :) – Blue Ice Jun 11 '13 at 06:21
  • This if condition call 3 functions. If first returns true, others two should not be executed. If first fails, then second should execute, and in similar manner third one. I doubting myself now about how OR operator works. – msinfo Jun 11 '13 at 06:23
  • Don't doubt yourself on it. You are correct about how the OR operator should work. However, for some reason Microsoft has decided to not make VBA work the way you explained in your comment. Most large programming languages implement OR as you described. (C, Python, C++) – Blue Ice Jun 11 '13 at 06:29

2 Answers2

2

You have to implement short-circuiting by hand:

result = function_one
if not result then
   result = function_two
end if
if not result then
   result = function_three
end if
If result Then
    ' Do Something
End If

VBA (and classic VB) doesn't offer short-circuiting versions of And or Or (In VB.Net, such items were introduced as AndAlso and OrElse)

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • I believe that my code is a bit more efficient because it does not assign values from functions to the variable "result". – Blue Ice Jun 11 '13 at 06:27
  • Since this post mentioned short circuiting and gave code example, marking it as answer. But comments / code of Blue Ice, and link from therefromhere gave more insight into this issue. My current skill of VBA doesn't let me to comment on efficiency of solutions prescribed. Thanks for answer and comments (short circuiting was new concept form me). – msinfo Jun 11 '13 at 06:40
1

This should be more efficient:

'define the do something function

If function_one Then
'do_something_function

ElseIf function_two Then
'do_something_function

Else function_three Then
'do_something_function

End If

Going off of your hunch, SBI. It should do the do_something function if function_one is true, and not check the others. I could be totally wrong, though, as I am not near my computer with Microsoft Office.

Blue Ice
  • 7,888
  • 6
  • 32
  • 52