How can I improve my macro that delete all headers&footers from thedocument?

A

avkokin

Whether Probably and How can I improve this macro? It delete all
Headers&Footers from all sections of active document. Thank you.
Sub test4()
Dim sec As Section
Dim hf As HeaderFooter
Dim rng As Range
For Each sec In ActiveDocument.Sections
For Each hf In sec.Headers
hf.Range.Delete
Next hf
For Each hf In sec.Footers
hf.Range.Delete
Next hf
Next sec
End Sub
 
J

Jonathan West

Well, it is clear enough what the macro is supposed to do. If it is
achieving that aim, then I don't see that there is necessarily any improving
that needs to be done.

Many people assume that there is a single "right" way to do programming. It
isn't so. It often happens that there are lots of ways of doing essentially
the same thing, and whether one particular way is better and the
alternatives depends on circumstances.

For instance, if you were writing this as part of a much larger project
being written by a team, there would probably be coding standards regarding
names of variables. You would be expected to change the variable names to
match that standard. Not because the other names make the macro run faster
or work more efficiently, but because a consistent naming convention makes
it easier for other people to understand what your code is doing if they
need to use it or modify it.

So, changing the names of the variables would be "right" in that
circumstance, but that is entirely a matter of local convention. It doesn't
make the code run any faster.

Even though I mostly write my code in a team of one, I do have a naming
convention I broadly stick to - it makes it easier for me to look back at
code I wrote a year or more ago and understand what I was thinking back
then.

So, I would write your macro a bit differently, but I don't think it could
objectively be regarded as "better".
 
A

avkokin

Well, it is clear enough what the macro is supposed to do. If it is
achieving that aim, then I don't see that there is necessarily any improving
that needs to be done.

Many people assume that there is a single "right" way to do programming. It
isn't so. It often happens that there are lots of ways of doing essentially
the same thing, and whether one particular way is better and the
alternatives depends on circumstances.

For instance, if you were writing this as part of a much larger project
being written by a team, there would probably be coding standards regarding
names of variables. You would be expected to change the variable names to
match that standard. Not because the other names make the macro run faster
or work more efficiently, but because a consistent naming convention makes
it easier for other people to understand what your code is doing if they
need to use it or modify it.

So, changing the names of the variables would be "right" in that
circumstance, but that is entirely a matter of local convention. It doesn't
make the code run any faster.

Even though I mostly write my code in a team of one, I do have a naming
convention I broadly stick to - it makes it easier for me to look back at
code I wrote a year or more ago and understand what I was thinking back
then.

So, I would write your macro a bit differently, but I don't think it could
objectively be regarded as "better".

Yes, thank you very much for your detailed answer.
 

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