LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 905
Default VBA: optimize If conditional expression?

I want to write:

If Left(s,2) = "Mr" Or Left(s,2) = "Ms" Then
...statements...
End If

But that will evaluate both Or operands even if the first one is true.

Is there a more efficient way to implement this?

Note: That example is a simplification. In actual practice, the
conditional expressions are more complicated. So don't try to optimize the
particular example. Optimize the paradigm.

My best:

doit = (Left(s,2) = "Mr")
If Not doit Then doit = (Left(s,2) = "Ms")
If doit Then
...statements...
End If

But that gets a little tedious. Alternatively:

Select Case True
Case Left(s,2) = "Mr", Left(s,2) = "Ms"
...statements....
End Select

But that seems a bit convoluted.

Am I overlooking the obvious?

 
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
How can I optimize this code? Gustaf Excel Programming 3 January 12th 09 12:43 PM
Conditional Expression in cell TPB Excel Worksheet Functions 3 April 4th 06 08:57 PM
Conditional Expression Question tristatefab Excel Worksheet Functions 4 March 20th 06 08:23 PM
Conditional Formatting OR Expression?? SMac Excel Discussion (Misc queries) 3 May 26th 05 05:10 AM
Optimize SumProduct Christopher Kennedy Excel Discussion (Misc queries) 9 December 10th 04 04:47 PM


All times are GMT +1. The time now is 12:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"