+ Reply to Thread
Results 1 to 5 of 5

Module vs Class Module

  1. #1
    Forum Contributor
    Join Date
    03-24-2005
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    378

    Module vs Class Module

    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?
    Thanks!
    Dennis

    I am using Windows 7 and Office 2007, all of my posts are based on this.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    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

  3. #3
    George Nicholson
    Guest

    Re: Module vs Class Module

    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/de...sesobjects.asp

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


    HTH,
    --
    George Nicholson

    Remove 'Junk' from return address.


    "DCSwearingen" <[email protected]>
    wrote in message
    news:[email protected]...
    >
    > 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?
    >
    >
    > --
    > DCSwearingen
    >
    >
    > ------------------------------------------------------------------------
    > DCSwearingen's Profile:
    > http://www.excelforum.com/member.php...o&userid=21506
    > View this thread: http://www.excelforum.com/showthread...hreadid=501243
    >




  4. #4
    Forum Contributor
    Join Date
    03-24-2005
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    378

    Thank You!!

    I am not a programmer by any means.

    I have used the help files and this forum to learn what I do know and I 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 was wondering how I could use it.

    I definitely need to do more reading.

  5. #5
    Jim Thomlinson
    Guest

    Re: Module vs Class Module

    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...
    --
    HTH...

    Jim Thomlinson


    "DCSwearingen" wrote:

    >
    > I am not a programmer by any means.
    >
    > I have used the help files and this forum to learn what I do know and I
    > 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 was
    > wondering how I could use it.
    >
    > I definitely need to do more reading.
    >
    >
    > --
    > DCSwearingen
    >
    >
    > ------------------------------------------------------------------------
    > DCSwearingen's Profile: http://www.excelforum.com/member.php...o&userid=21506
    > View this thread: http://www.excelforum.com/showthread...hreadid=501243
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1