Practical Macro Size Limit?

G

Guest

I'm building a Macro that is growing "like Topsy". At the moment it is 145
lines long, and will increase by about 50% from there when done. Because I'm
an old (really OLD<g>) VFP programmer, my thinking and development pattern is
shaped by that experience.

So, is it considered bad form to have a macro get large? I recall that in
C++ the recommendation was to try to hold a process to one screenful where
possible, and then build a "parent" process that called the segments in
sequence. However my macro consists of a series of nested For Loops that goes
five or six levels deep.

Just wondering -- and trying to learn <g>!
 
G

Guest

I said I was OLD, couldn't remember the word "module" to save my life. Not
ignorance, just a little "Halfzheimer's" <g>). Non "Process" est, sed
"Module". Mea culpa <g>!
 
G

Guest

There is a limit of about 64K on the size of a module. You would export it
to a .bas file, then look at the size of the file. The limit is not
published anywhere, but appears to be a practical limit - above that you
start having problems. Beyond that, I don't believe there is any limit.
Some people create huge procedures using the macro recorder.

Use of subroutines is encouraged from a maintenance/readability standpoint.
 
G

Guest

Use good programming practives. Usually around 200 - 250 line of code per
module is the limit. Better than the 500 punch cards I had to use in
college. Consider how you are going to tet the code in making the decision
to have one or multiple modules.

You didn't say if you where building a subroutine or a function, but it is
always good to create sub-functions where possible.
 
G

Guest

Ok, so to use sub-modules (and I really like that idea), suppose I have this
construct:

Dim blnYunky as Boolean
Dim myParameter as Long
For Each Yadda In Yading To Yadong
If Bingo Then
'what do I put here to call module Fooby with a parameter?
blnYunky = MyModule(myParameter, myResult) '??
Endif

Private Function MyModule (ByRef myParameter As Long, _
myResult As Boolean) As Long
For Each rngWiggy In Range(rngBugfree(1), rngBugFree(myParameter)
'Yadda, yadda
If MySomething Then
MyResult = True
End If
Next rngWiggy
End Function

Is that the sort of thing you mean?
 
G

Guest

Yes, except MyModule doesn't return anything.

Dave Birley said:
Ok, so to use sub-modules (and I really like that idea), suppose I have this
construct:

Dim blnYunky as Boolean
Dim myParameter as Long
For Each Yadda In Yading To Yadong
If Bingo Then
'what do I put here to call module Fooby with a parameter?
blnYunky = MyModule(myParameter, myResult) '??
Endif

Private Function MyModule (ByRef myParameter As Long, _
myResult As Boolean) As Long
For Each rngWiggy In Range(rngBugfree(1), rngBugFree(myParameter)
'Yadda, yadda
If MySomething Then
MyResult = True
End If
Next rngWiggy
End Function

Is that the sort of thing you mean?
 
G

Guest

To expand on what Tom said. One of the biggest reasons to break up a large
procedure into a set of small procedures is readability. You alluded to that
in your original post. It should all fit on one screen. Debugging is a whole
lot easier on small procedures that do a limited number of things. My general
rule is that one procedure should do one thing. For instance it could format
a sheet or copy some data or print one or more sheets. If I want to do a
bunch of things then I use a main calling procedure to call my format precdue
then copy then print. If I need to debug or modify my code it is easy to
determine which procedure or procedures need to be modified. So in general
you should reach the proctical limit for the size of a procedure long before
you hit an technical limit... Just my 2 cents.
 
G

Guest

This is all wonderful for my learning curve. Now my naivitee will emerge:
Where do you park the subProcedures? For example I am currently working in
Module1 (Code). If I park my sub-procedures in the same Module, even though I
have made the code easier to read and to debug, am I not still at risk of
hitting that size limit previously mentioned by Tom?

Otherwise, how do I initiate a new Module to stash my sub-Procedures?
 
S

Susan

just stash 'em in another module with "public" before them - then any
module (or userform code) can call them.
while you can't name a module the same name as the sub inside it, i
try to name it something similar so i can keep track of them.

so if a module contains:

sub find_last()
end sub

then i might name the module
find_last_integer

or something like that.
susan
 
G

Guest

You can park them anywhere you want. VBA will find them by name, which it
has already sorted and listed in the declarations dropdown on the VBE window.
I was just wondering if you were ever in Mahe, Seychelles?
 
G

Guest

«I was just wondering if you were ever in Mahe, Seychelles?» Closest I've
been was 2.5 yrs atop the Rock of Gibraltar!
 
G

Guest

Try to place like procedure in their own modules. you can add as many module
as you want by selecting Insert -> Module in the VBE. It is a good idea to
name your modules something descriptive like modSave, or modPrint. Place all
of your saving procedures in modSave and all of your printing procedures in
modPrint. YOu could have a main calling procedure that looks like this

Public Sub DoStuff
call modPrint.PrintPage1
call modSave.SaveAsNewFile
end sub

Easy to follow and easy to debug if something goes wrong...
 
G

Guest

Crazy learning curve -- but, OTOH, because I am working on this as a Temp,
should anyone else ever try to decipher what I've cobbled together, I would
like to build it according to "standard practice". I have a shocking and
embarrassing confession to make too, I'm actually commenting every row! Of
course part of that is to help the atrophying grey cells to remember what the
Sam Hill I was thinking when I put it together in the first place <g>!
 
G

Guest

Here is a sample of how modular code can work in VBA. The first one is the
control
sub which calls the others in the order they need to run. The sub routines
are listed in order but they don't have to be. They can be anywhere in the
standard module.

Sub addErase()
addCellValue
moveCellVaule
changeCellValue
delCellValue
End SuB

Sub addCellValue()
Range("A1") = "Hello"
End Sub

Sub moveCellValue()
Range("A1").Value.Cut Range("B1")
Application.CutCopyMode = False
End Sub

Sub changeCellValue()
Range("B1").Value = Range("B1").Value & "World"
End Sub

Sub delCellValue()
Range("B1").Clear
ThisWorkbook.Save
ThisWorkbook.Close
End Sub
 
G

Guest

Thank you (and everyone) for all the help on this. As I am primarily a
"practical" (vs. "theoretical") learner, the example you gave was a huge
help. I really appreciate everyone's patience with me <g>!
 
G

Guest

«then i might name the module»
I just Inserted a new Module, but I can't seem to find where I name it. Also
it Started out with "(Declarations)" As the only choice in the right drop
down,a nd "(General)" for the left (also only one). Am I supposed to be
allowed to tweak these?
 
G

Guest

Depending what I am up to I will ususally comment every 3 to 5 lines of code.
When I did a stint temping (as you are) I commented like there was no
tommorow. Every module, every procedure, every variable and at least every 3
lines... It makes the code much more serviceable and heck you are getting
paid by the hour!!!
 
G

Guest

Empirical testing tells me I can't push my Dims or my initializations out to
a sub -- correct?

Public Sub myProject
Dim A As Long
Dim B As String

Call doInits
End Sub

Public Sub doInits
A = 5
B = "Bingo!"
End Sub
 
R

Roger Govier

Hi Dave

No you can't, but you could initialise a set of values with
Const A as Long =5
Const B as String ="Bingo"
which could sit outside of and before your subs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top