That arguably raises the delicate (?) issue of efficiency over practicality ...
I'd nearly always opt for Select Case myself as I much prefer the structure and simplicity of syntax ... ordering of the Cases is of course important (and no Like allowed of course).
In terms of efficiency over practicality I'd throw Evaluate into the mix as an alternative to Iteration (where viable).
Bill Jelen (Excel Gurus Gone Wild) illustrates the potential performance variance using a simple example of inverting the sign of 100,000 cell values.
Using a Loop this takes around 8 seconds whereas Evaluation takes less than a tenth of a second.
(please note I'm not saying you would actually iterate to invert the values but still... it's a useful example)
In reality compared to the most basic of Loop processes Evaluate is neither intuitive nor trivial to adapt [and as R has pointed out to me previously - needs to account for default Ref. Style]
(you could probably argue the time spent setting it up would exceed the benefits gained at run time)
So there should be some sort of trade off in IMO.... some practices are no brainers... other choices are not quite so clear cut, at least not for me.
Sorry if I'm simply repeating prior comments...
Last edited by DonkeyOte; 02-19-2010 at 11:54 AM.
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
Ah yes, I remember seeing this for the first time in one of your posts: http://www.excelforum.com/excel-gene...ork-sheet.html
Regards
Jon (Excel 2003, XP Pro)
I totally agree. If you were to code for pure performance, you would only ever use inline code rather than separate single-purpose subs/functions and maintaining it would be a nightmare!
(of course if you wanted real performance you wouldn't be using VBA, but that's by-the-by.)
So long, and thanks for all the fish.
Hearing that If...ElseIf...Else is more efficient than Select Case is interesting, as every one of my programming teachers in college said the exact opposite. They're reasoning was that Select Case structures only check for the variable's value once and then perform each of the cases in order, whereas an If/ElseIf has to test it for every "case" as it were. If you have a Select Case with 20 cases and one variable test at the beginning vs If..ElseIf..Else with those same 20 tests done in order, one (ok, me) would think the Select Case is faster.
Have I been led astray?!?![]()
Regarding IIf: I use the structure frequently if the evaluation is trivial. Compactness and clarity have their own value:
Code:sName = IIf(bFlag, "Bob", "Joe")
I am never writing five lines of code to do that, or even this one:
Code:If bFlag Then sName = "Bob" Else sName = "Joe"
Re Case vs If:
One of the first computers I programmed was an HP minicomputer in Fortran. The compiler had a toggle that would generate a mixed listing as output: Each line or two of Fortran was followed by the exact assembly code it generated. It taught me how to both write efficient Fortran and how to write in HP Assembler (... and how to write self-modifying code, but ignore that). To bad VBA doesn't do the same, even if the output were not assembler but whatever virtual machine code the compiler uses.
And all that said, I use Select Case in preference to If/ElseIf unless I really feel it's important. It's more visually apparent.
Stephen, I couldn't agree more. It makes program flow much more apparent, and makes orderly code look orderly, which is one of the nicest gifts you can give yourself or the other poor dog that picks up your code a year later.Originally Posted by StephenR
Last edited by shg; 02-19-2010 at 01:46 PM.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Where is the sub-quoted section of Stephen's when I try to "Quote" the Shg's last post?
Is this a known issue...?
(I think I'll add it to the feedback thread & see what happens)
Yep, me too & I haven't had much chance to gift my work to others... yet! ;-)
I try to run people's code through Bovey's smart indenter.xla before posting, whenever the code is of reasonable length/levels. When I can see people are really keen to learn (& I remember) I suggest Smart Indenter, Code Cleaner & MZ Tools (esp after RS pointing out some extra tricks that it contains).
Rob Brockett
Kiwi in the UK
Always learning & the best way to learn is to experience...
Thanks shg, but I merely collated the list from others' contributions. However, I do observe that stricture. The aesthetics of VBA is a much overlooked topic...
I think with many of these things, the old adage about theory and practice comes into play!![]()
So long, and thanks for all the fish.
IMO, it's like playing bridge; play third-hand high, and return your partner's leads, until you clearly understand why not to on any given occasion.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Learn to use the :
- debug tools especially F8
- the object browser
- help
and when nameing variables think about how they will read eg:
not
but either ofCode:bReset = TRUE 'does this mean the reset has been done or need to do a reset if bReset then
Code:bIsReset = TRUE if bIsReset then bDoReset = TRUE if bDoReset then
Hope this was useful or entertaining.
I think it's complet, i don't think that there are any missing point in this:
What i want to add is just this, it turn off Evrything but the Essentials while your Code is Runnig, doing this should help improve the performance of your code:
From http://blogs.msdn.com/excel/archive/...practices.aspxCode:'Get current state of various Excel settings; put this at the beginning of your code screenUpdateState = Application.ScreenUpdating statusBarState = Application.DisplayStatusBar calcState = Application.Calculation eventsState = Application.EnableEvents displayPageBreakState = ActiveSheet.DisplayPageBreaks 'note this is a sheet-level setting 'turn off some Excel functionality so your code runs faster Application.ScreenUpdating = False Application.DisplayStatusBar = False Application.Calculation = xlCalculationManual Application.EnableEvents = False ActiveSheet.DisplayPageBreaks = False 'note this is a sheet-level setting '>>your code goes here<< 'after your code runs, restore state; put this at the end of your code Application.ScreenUpdating = screenUpdateState Application.DisplayStatusBar = statusBarState Application.Calculation = calcState Application.EnableEvents = eventsState ActiveSheet.DisplayPageBreaks = displayPageBreaksState 'note this is a sheet-level setting
What do you think we should add to this to have a complet page of all the things that we should avoid in Vba
Something else just thought of...
If replacing formula with values rather than use copy...paste special...values use .value = .value e.g.:
DomCode:With Range("A1:A10") .Value = .Value End With
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
If you haven't already please take some time to read the Forum Rules.
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
This is one of my favorite tricks to use. Any arguments against it?Code:.value = .value
_________________
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 use it too, but what is the argument for it? Is it quicker?
I remember seeing a discussion about it on Mr Excel which I'm sure suggested it should be used in preference to paste special.
I'll see if I can find it and post the link.
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
If you haven't already please take some time to read the Forum Rules.
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks