ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Opinion on Size/Length of Macro/Function/Sub (https://www.excelbanter.com/excel-programming/371479-opinion-size-length-macro-function-sub.html)

Rob

Opinion on Size/Length of Macro/Function/Sub
 
Should I have 13 seperate Macros or one long Sub/Macro??

I was wondering if I can get some opinions on this subject. The reason is
because I have a Workbook that has a Macro/Sub that has 600 lines of code in
it. But here's the thing... The code I have performs like 13 different
routines that could be broken out seperately.

Your opinions are most certainly valuable to me so please enlighten me.


Thanks Tons.
Rob

Jim Thomlinson

Opinion on Size/Length of Macro/Function/Sub
 
Where to begin... 600 lines is almost definitly too long. How long should it
be. That is another matter. One theory is that one procedure/function should
do one thing. It makes the procedure easy to follow and document. Generally
speaking all of the code will fit on one printed page and that will make it
easy to debug. That being said this is not always feasable or desirable. My
general rule is that a procedure should do one type of thing. By that I mean
things like data extraction, data manipulation, formatting, printing,
saving... This keeps the code nicely compartmentalized. If something goes
wrong I can quickly narrow it down to the procedure that is causing the
problem. The other reason to keep your functions down to doing one thing or
one type of thing is that it will allow you to reuse your code. A bunch of
different procedures may do a bunch of different things but they may all call
the same save procedure for instance.

As with all rules these rules are made to be broken. But before you go
breaking rules make sure you understand why the rule is there and that you
have a good reason for breaking it. When to break the rules is one of the
hardest rules to follow.
--
HTH...

Jim Thomlinson


"Rob" wrote:

Should I have 13 seperate Macros or one long Sub/Macro??

I was wondering if I can get some opinions on this subject. The reason is
because I have a Workbook that has a Macro/Sub that has 600 lines of code in
it. But here's the thing... The code I have performs like 13 different
routines that could be broken out seperately.

Your opinions are most certainly valuable to me so please enlighten me.


Thanks Tons.
Rob


JLGWhiz

Opinion on Size/Length of Macro/Function/Sub
 
As a novice programmer, I second Jim's advice. If you can modularize your
code it makes it a lot easier to manage, especially if you have a routine
with a lengthy algorithm that you need to run more than once. I cut one of
my first programs in half after I learned how to call the sub routines from a
master sub. Even if it is a file save exercise you can use a one line call
as opposed to three lines or more each time.

"Rob" wrote:

Should I have 13 seperate Macros or one long Sub/Macro??

I was wondering if I can get some opinions on this subject. The reason is
because I have a Workbook that has a Macro/Sub that has 600 lines of code in
it. But here's the thing... The code I have performs like 13 different
routines that could be broken out seperately.

Your opinions are most certainly valuable to me so please enlighten me.


Thanks Tons.
Rob



All times are GMT +1. The time now is 07:58 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com