This was covered vaguely from around post #72 ... ie efficiency versus practicality.Originally Posted by pierre08
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
The point is somewhat academic unless you've got the Select statement inside a loop executing a zillion times. It will take the next programmer more time to figure out what efficient but weird code does than time saved in execution for the user.
Making the world a better place one fret at a time
||||||
If someone helped you, please click on the star icon at the bottom of their post
If your problem is solved, please update the first post:
EDIT, Go Advanced button, set Prefix to SOLVED
[code]
' Enclose code in tags like this
[/code]
Don't attach a screenshot--just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.
Not sure about the instructors, probably just teaching what they've been taught.
But when I'm using F8 to step through my code, Select Case and If...ElseIf...Else...End If both do the same thing...they go through the options in order listed in the code and as soon as a TRUE evaluation occurs...it skips out thus not testing any IFs or CASEs that followed.
So I wonder where the increased efficiency for one over the other even comes from?
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
I always assumed the the difference was that "Case" exits when the first true statment is found
and If...ElseIf...Else... evaluates all the statments ..
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
No, I just ran this and it skipped to the end when it found "2" in [A1]
Sub test() If Range("A1") = 1 Then Range("B1") = 2 ElseIf Range("A1") = 2 Then Range("B1") = 3 'stopped here ElseIf Range("A1") = 3 Then Range("B1") = 4 ElseIf Range("A1") = 4 Then Range("B1") = 5 End If End Sub Sub test() Select Case Range("A1").Value Case 1 Range("B1") = 2 Case 2 Range("B1") = 3 'stopped here Case 3 Range("B1") = 4 Case 4 Range("B1") = 5 Case 5 Range("B1") = 6 End Select End Sub
Last edited by JBeaucaire; 02-26-2010 at 04:33 PM.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Both evaluate only to the first True condition. You know that, you just didn't stop to think it through.I always assumed ...
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Hard to say without seeing the generated code, but in both statements you have to evaluate dynamic expressions. Hard to say why either one would have a significant advantage. I suspect if there's a difference it would be somewhat academic. The IF statement require it to evaluate one expression per branch, but the Select requires it to evaluate one expression on the Select line, plus one expression per branch, so it has to do a little more evaluation.
(You compile VBA so I assume it must generate code, though I have this nagging feeling it's interpreted.)
Making the world a better place one fret at a time
||||||
If someone helped you, please click on the star icon at the bottom of their post
If your problem is solved, please update the first post:
EDIT, Go Advanced button, set Prefix to SOLVED
[code]
' Enclose code in tags like this
[/code]
Don't attach a screenshot--just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.
A nice simple illustration here. If you step through it you will notice that the function myF() gets called twice in the "If ... elseif" and only once in the case statement. Which must mean more work is being done in the "if ... elseif". This will be compounded by more complex conditions and increasing numbers of elseif.
But of potentially greater concern is that the value of test maybe different at each elseif. For example a time based test will have a different value for the comparison at each point. Whereas the "case" statement will only use one value for comparison.
Additionally there is always the possibility that the elseif is not testing exactly the same condition a myF1("0") and myF1("O") may easily be overlooked. Using a select case ensures that only the one value is being used as the source.
Sub main() Dim sI As String Dim sC As String Do Range("a1") = "B" If myF() = "A" Then sI = "if 1 " ElseIf myF() = "B" Then sI = "if 2 " Else sI = "IF is not matched " End If Range("a1") = "B" Select Case myF() Case "A" sC = "case 1" Case "B" sC = "case 2" Case Else sC = "case not matched" End Select Loop While MsgBox(sI & sC, vbOKCancel) = vbOK End Sub Function myF() As String myF = Range("a1") Range("a1") = InputBox("enter value") End Function
Hope this was useful or entertaining.
just in case (excuse the pun) it needs saying: it is also obvious that the conditions in the "case" statements could give rise to further dynamic conditions. These should, in my view, be avoided.
My main point was to illustrate that the myF function gets evaluated on each line of the "IF" but only once in the "case"
Hope this was useful or entertaining.
A slightly contrived example, IMO, since in reality you would store the value in a variable at the start then compare the variable in the If statements, just as in the Select Case, unless you actually had a need to re-evaluate it each time.
Note: in languages like C++, I believe that switch statements are more efficient due to the way the compiler optimises them.
Last edited by romperstomper; 02-26-2010 at 06:43 PM.
don't eat yellow snow !
"Unless otherwise stated all my comments are directed at OP"
Mojito connoisseur and a dabbler in Cisco
where does code go ?
look here
how to insert code
how to enter array formula
why use -- in sumproduct
recommended reading
wiki Mojito
how to say no convincingly
most important thing you need
Martin Wilson: SPV
and RSMBC
And if the return value of myF() could vary within the same code block (e.g., Rnd()), it would be a very wacky piece of code.A slightly contrived example, IMO ...
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
That's true, but it's not really a demonstration of the performance of Select Case versus If... Else.![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks