View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
onedaywhen onedaywhen is offline
external usenet poster
 
Posts: 459
Default Function Vs. Sub

There are a few scenarios where your procedure must be a sub:

• to allow a user to run the procedure as a macro i.e. Tools, Macros,
Macro: only public subs in standard modules without Option Private
Module will appear in the macro list;
• to assign a procedure to a control (Forms toolbox), shape,
hyperlink, etc;
• events provided by the application (Workbook_Open, Worksheet_Change,
etc) are always subs and can't be changed to functions.

My personal preference is to use functions for all but my top level
procedures, using a Boolean = True return value by default. The
calling procedure can choose to ignore a return value, effectively
treating it as a sub.

Also worth pointing out are the other sub procedure types available to
you: Property Get, and Property Let (Property Set for objects).

"mars" wrote in message ...
Hello,

As a novice, I would like to know if someone can tell me
what the guidelines/best programming practices are on when
one should use a "Function" versus a "Subroutine".

It appears to me that both structures are completely
interchangeable. Is there any speed, memory overhead, or
other performance issues?

Perhaps someone can offer a short discussion, or point me
to web site? Thanks for the help in advance.