Re: Maximum Length For A Macro
Whoa, a 2000 line procedure! That has to be a pain to maintain.
My biggest VBA program is about 2500 lines, not including comments, but that is split to 62 different procedures in standard and class modules, averaging to about 40 lines of code per procedure. The average lenght of the procedures is dragged down by some basic string, date and file handling functions and let-get-set statements, but in general, no procedure exceeds the 100 line limit.
Here's a few hints you might consider incorporating in your coding:
Any task you need to perform more than once should be it's own function. Sometimes when you only need to something once, writing and calling a function still pays off, because you can declare all the necessary variables to perform the tasks within the function, and you can have them destroyed immediately, which will save your memory resources.
Write as general functions as possible, so they can be reused. If you need to for instance extract a pattern from a string using regular expressions, don't hardcode the pattern into the function, but pass it as an argument. That way you have a wrapper function that you can call with a single line of code from any procedure you ever write. This also achieves the fact that as you use a procedure, you start coming up with new ideas on how to do it better, discover bugs or errors, that you can then fix, and the overall quality of your code will improve over time. Look into MZ-Tools to start your own code bank.
As a rule of thumb, each function should do exactly one thing. This will increase the reusability of your code. If you ever find yourself returning multiple results ByRef, you probably should do it with separate functions anyway.
Similarly, if you need to do formatting for a data table, write a separate sub to do it. After that, when you need to get data from a database to the sheet, have a separate function to create the database connection, etc. In general the main procedure you or your users call should be a set or directions, and the actual low-level handling should be for the most part done by functions and subprocedures.
Just my two cents.