View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Nigel Nigel is offline
external usenet poster
 
Posts: 923
Default Macro, Module, function, sub and This workbook and sheets ?


"Hari" wrote in message
...
Hi,

For the last couple of days I have been indiscriminately pinching codes

from
NG and pasting it in to This workbook or by inserting new modules without
understanding which should be used for what.

I am a newbie and want to learn VBA with excel.

Couple of questions based on this:-

1. What is the difference between Sub, Function, Module and code.


Sub is a procedure of collection of code that can be executed from within a
module, workbook, or worksheet
Function is a special from of sub that returns variables, and is most often
(but not exclusively) on a worksheet to provide functions not in the
standard Excel functions list.
Module is one lace to store Subs and Functions
Code is a group of VBA instructions, making up a Sub or Function

2. If I get a code ( from NG) where should I add it . Should I add it

"This
workbook" or a new module. If possible please tell me in what

circumstances
one should be adding the code in which of the above 2


The place depends on the purpose of the code on the one hand and the project
design on the other. In most cases code would be placed in modules, but if
the code is triggered by a workbook, worksheet or userform event then it is
placed in the the respective area.


3. What is the difference between Public, private with reference to both

sub
and function.

In general, think of Public as refering to variables that are available to
all codes, and Private to the codes residing in the same place.


4. When do we add codes to "Sheet1" rather than workbook.

See note above ref 2. If the code is specfic for a worksheet it will need
to be placed the sheet it is triggered from, not just "Sheet1"

5. Based on info from newsgroups I learned that a Macro/Module ( I believe
macro is recorded and module is anything which is written, please correct

me
if am wrong) could be called. Can we call both a macro and function from
another function/macro

Macro is a general term for code (more commonly called a Program), most
often meaning a Sub. Wherever it is stored and however it is created -
written or recorded it makes no difference.
Yes, in general you can call a function/macro (Sub) from another Sub but not
from a function.


6. Can a module in sheet call a module in another shet/workbook or from
module 1. -

Yes

Please guide me to the right source for learing these bsic concepts if
possible.


Michael Kofler - A definitive guide to Excel VBA - Apress ISBN 1-59059-103-8
or any of the other introductory and references for Excel VBA, be aware the
different versions of Excel have different features and functions. Mostly
they are forward compatible, in other words code written in an older version
will work in a newer version (not always!)


Regards,
Hari
India