Module vs Class Module

D

DCSwearingen

I have gone to the VBA help files to try to find the difference and I am
no better now than before.

A Class Module:
A module that contains the definition of a class, including its
property and method definitions.

A Standard Module:
A module containing only procedure, type, and data declarations and
definitions. Module-level declarations and definitions in a standard
module are Public by default. A standard module is referred to as a
code module in earlier versions of Visual Basic.

What is the purpose of a class module and when should it be created?
 
L

Leith Ross

Hello DCSwearingen,

Class modules are used to create custom objects. These objects don't
have to be visible like a Window or control. They can be objects like a
Collection. The objects have Properties that are defined by the Let,
Get, and Set statements in VBA. Methods are created using Subs and
Functions. These can be either Public (Global in scope) or Private
(Local in scope - for the Class' use only). Class modules are sometimes
referred to as "In-process Servers".

You can create one whenever discover you need an object to perform
certain functions that don't already exist. To use the Class object,
you use the Dim statement. For example: Dim myObj As New MyClass

The first reference to the object in your code will create an instance
of the object. The object's lifetime depends on where it delcared, just
like other objects and variables. This is just an overview and not a
thorough explanation , but I hope it helps you to better understand
what a Class module is.

Sincerely,
Leith Ross
 
G

George Nicholson

Simply, a Class module serves as the "cookie cutter" of an object.

Example: behind the scenes, Excel has class modules for Workbook, Worksheet,
Range, Chart, etc. objects. Every time you reference any property or method
of those objects, their Class modules determine how Excel responds/behaves
(i.e., return or set a value, do something else).

One reason to create your own Class module would be if you wanted to create
an object with it's own Properties and Methods. Say a class for Customer,
Account, OrderItem, Transaction, Employee, Task, etc. An object is a data
structure that can be held in memory and can be easily manipulated and will
behave according to your pre-determined business rules and instructions:
Customer.Address, Transaction.Date, Employee.Age, OrderItem.Price,
Customer.UpdateBalance, Customer.AvailableCredit etc.

If something is going to be subject to a lot of processing (and/or you have
a lot of 'somethings'), loading info into a class object, putting that
object into a collection and then doing your processing can be a lot faster
and efficient than repeatedly reading & writing data to a spreadsheet.

And, aside from application-specific issues like Input & Output, a given
Customer class will behave the same in Excel or Access, so re-use and
portability can be an advantage as well.

All in all, this might come under the heading of: if you need it, you'll
know it. :)

Microsoft Office XP Developer's Guide: Chapter 11: Custom Classes and
Objects
http://msdn.microsoft.com/library/d...us/modcore/html/deovrcustomclassesobjects.asp

VBA Developers Handbook: Chapter 6: Creating Dynamic Data Structures Using
Class Modules
http://msdn.microsoft.com/library/d...ingdynamicdatastructuresusingclassmodules.asp


HTH,
--
George Nicholson

Remove 'Junk' from return address.


"DCSwearingen" <[email protected]>
wrote in message
 
D

DCSwearingen

:) I am not a programmer by any means.

I have used the help files and this forum to learn what I do know and
am starting to use some of the things I pick up here.

I saw the Class module when I was inserting a new module and wa
wondering how I could use it.

I definitely need to do more reading.;
 
G

Guest

In 99% of all cases you don't need to worry about class modules. Regular
modules will do very nicely. Exceptionally little of the code presented on
this site is designed for class modules. Most of the code you will find
anywhere for VBA is for regular modules. If you are just learning you should
probably just stick with regular module and the "Procedural" code that they
use. Just my two cents...
 

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