VBA Philosphy

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071
OK I am getting reasonably competent at writing VBA to do what I want to, but because I am all self taught there certain more technical details I am still missing. Here is one:

Frequently I can reduce the overall number of lines in my code through careful use of for and do loops, however I was think about it and realized that use of a loop means you have to use variables and you also have two additional lines of code (the for/do and the next) that must be executed each loop. Is it better to have fewer lines and more line execution, or more total lines but fewer lines executed?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
With non-VB programmes I have written I always preferred less lines of execution code on the assumption (see next sentence) that it would be faster. If performance is the issue then use less lines of execution code - not forgetting to minimise the number of file reads and writes, which are two of the slower tasks. If space is the constraint (which I doubt given the size of HDD's of today) then go for less lines of code in total.
My 2c
Andrew :)
 
Upvote 0
Hi,

Unless you are creating time consuming simulations, or something else that really taxes Excel/VBA, write your code so that it is easily readable for the human interpreter, rather than the computer.

My bet is that easily maintainable code will benefit you and others far more than code that is optimized to the nth degree. Of course, well written, efficient and easily maintainable code can be optimized for performance, but the focus should be first on easily understood code rather than all the speed tricks.
 
Upvote 0
For this specific task I will be the only party mainting the code and it perorms some time instensive tasks... Hence me wanting to optimize it to the nTh degree. Besides... It's just fun :lol:
 
Upvote 0
My 2 cents --

I agree with Jay -- make the code easy to read and maintain, especially if you're not the only one looking after it

I tend to use loops where possible for repetitive processing, but sometimes an inbuilt Excel feature is much quicker -- for example, using FillDown or selecting a range and inserting the same formula in one hit. Or, using one of Excel's built-in functions to return a value for you.
Then there's the obvious stuff like turning off screen updating, and/or setting calculation to Manual.
And if you have heaps of variables to pass to a looping routine, read them into an array and feed them into the loop. That can be easier to maintain too, because you can just set up a table in a (hidden) worksheet

Denis
 
Upvote 0

Forum statistics

Threads
1,224,856
Messages
6,181,424
Members
453,039
Latest member
jr25673

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top