Francis Hookham said:
Many thanks, J E McGimpsey and Bernard Rey - self taught, mostly by trial
and error (mostly error), from the earliest days of Multiplan, I am an
amateur but enthusiastic XL user as earleir questions have shown - all my
macros to date have been written within Module modules. Now you introduce me
to macros in the Workbook codesheet
1
Is it possible for you give me (or guide me to) an explanation of macros in
codesheets of Modules, the Workbook or I imagine a specific Worksheet and
rule of thumb about which type of macro to place where?
Worksheet and Workbook code modules are specific types of Class
modules. Clas modules, unlike the regular modules you've been using,
can receive Event notifications from XL for certain events, such as
SelectionChange, BeforePrint, Activate, etc. For a list of which
events are recognized, open the module, set the left-hand dropdown
to Workbook/Worksheet, and look at the choices in the right-hand
dropdown.
If there is an event-handler (i.e., event macro) defined for that
event, it is run automatically. For instance, if the worksheet
module contains a Worksheet_Change() event macro, then every time
the user makes an entry (or a remote source changes the entry), that
macro will be run. Some of these macros have built-in arguments,
such as
Private Sub Worksheet_Change(byRef Target As Excel.Range)
where Target is the range variable that refers to the changed cell.
Another difference between Workbook/Worksheet and regular modules is
that the object reference "Me" is defined, which refers to the
object (Workbook or Worksheet). So instead of
ThisWorkbook.Close
that you would use in a regular module, in the ThisWorkbook module
you could use
Me.Close
instead.
Another key difference is the way that unqualified references are
handled. In a regular module
Range("A1")
refers to cell A1 on the ActiveSheet. In a Worksheet module, that
unqualified reference refers to that worksheet.
For that reason, placing general macros in the Thisworkbook or
worksheet code modules can cause some unexpected behavior. I
strongly recommend placing only event macros in the ThisWorkbook and
Worksheet code modules, and placing other macros in regular code
modules. You can also place all your macros in regular code modules
and call them from your event macros. Event macros, obviously, have
to be in their respective class modules.
For more, see
http://www.cpearson.com/excel/events.htm
2
Bernard Rey wrote 'In your case, I'd suggest you change your "Auto_close"
macros (considered as "old-fashioned") for a handy "BeforeClose" event
procedure
' which is interesting but difficult for me to see the
difference
Couple of differences: Automacros will not run automatically if a
workbook is opened from code - you need to use the RunAutoMacros
method.
Automacros are also only supported for compatibility with
pre-XL97/98 versions. That doesn't mean they're going away anytime
soon (XL4 macros still run, after all), but there's no guarantee.
Also, the BeforeClose event has a Cancel argument which allows you
to cancel the closing of the book if your conditions aren't met.
3
Private, Public or not is also an area I am uncertain about - any guidance
please
Macros declared Private are only accessible within the code module
where the macro resides. Event macros are by default private, since
it doesn't make sense to call them from outside - they're triggered
by events. Public macros are accessible from anywhere in the
project, and will show up in the Run Macro dialog. If I have a macro
that uses several submacros, I often make the macro public and the
submacros private so that the user doesn't see the submacros.
4
All my Personal Macro Workbook macros are in Module codesheet - should I be
putting these macros in the Workbook codesheet
Almost certainly not - I'd recommend keeping them in regular code
module(s).
In my Personal Macro Workbook, I have a Workbook_Open event macro
that calls a series of other macros in regular code modules which
load add-ins, replace toolbars and menus, reconfigure the keyboard,
initiate a class object to automatically remove hyperlinks, and set
certain preferences. This is because I like a highly customized work
environment. I also have a Before_Close event macro that undoes most
of those things. Most users don't need or want to do those things.
I also have a boatload of macros in about 5 different regular code
modules, organized by function or purpose. These are the ones that
are called from my keyboard shortcuts or toolbar buttons, though I
put most of those routines in add-ins.