+ Reply to Thread
Results 1 to 11 of 11

What is a Class Module

  1. #1
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Question What is a Class Module

    What the heck is a Class Module? Where do they come from? What are some practical applications?
    I'm looking for a few breadcrumbs on where to begin or how to learn this stuff, outside of simply Googling those two words.

    Maybe some examples, a roadmap, or a story of how others have gone down this path.
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,454

    Re: What is a Class Module

    http://www.cpearson.com/excel/classes.aspx


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: What is a Class Module

    Class method concept is the same as in most OOP(Object programming languages). Class could be one of the following:
    Classification
    Describes a thing
    Blue print
    Noun
    It has properties and methods.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: What is a Class Module

    Quite simply they allow you to create objects, you are in all likelihood already using class modules without realising it - Sheets, Userforms, This_Workbook are all examples of Class Modules.

    The primary reason that they're used in VBA is to listen for events, to put this into context, what are commonly thought of as "Events" in VBA :

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Etc, are not events. What's happening is that The sheet, or userform object is raising an event that the object that is created by the Sheet Class or Userform Class is listening for. AS it's listening (or subscribing to), it can run a sub when it happens - Hence the above code just being normal subs.

    So why is this useful? Or more specifically, why would you want to create your own?

    More often than not in VBA, for extending controls. Consider wanting to have a textbox only accept numeric input, one way would be to have something like this:
    Please Login or Register  to view this content.
    Which is fine, but it's usually neater to do all your setting up at the beginning of a userform, so we could write a class like this:
    Please Login or Register  to view this content.
    We can then loop through all the textboxes in the userform and make them numeric only - in this case by looking at how they're named:
    Please Login or Register  to view this content.
    This means that there's no need to add the additional OnlyNumbers call in each Change Sub and keeps the userform clean of code that doesn't relate to its function. It also allows us to dynamically add more textboxes at run time and make them numeric only - this isn't something that's done without classes.


    There are other reasons to use classes too, but they relate to the way you structure your code, breaking it into logical chunks that makes for easier testing and refactoring - though one technically doesn't need to use classes for those, it makes things easier.

  5. #5
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: What is a Class Module

    Thanks Kyle, that exactly what I needed.

    I found a class module the other day with WithEvents in it and didn't know it was reserved, which was watching for changes in formatting.

    I'll see if I can find a list of watch words, such as _Change to see what types of actions be listened to.

    Things I learned online today:

    WithEvents
    • Control data-type
    • _Change
    • The color blue

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: What is a Class Module

    There isn't a definitive list, each object you are subscribing to can raise different events.

    You can simply see the list of available events by a withEvents declaration in a class module and then using the regular dropdowns immediately above the code editing window

  7. #7
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: What is a Class Module

    I was coding for over a year before I discovered the different Worksheet and Workbook events in the dropdown menus.

    Good to pick up on this in a slightly quicker fashion.

  8. #8
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: What is a Class Module

    Control doesn't show up on the Intellisense list. That's mean.

    If I manually fill it in with lowercase, it still corrects to "Control" though. More research is required.

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: What is a Class Module

    Sorry, what control?

  10. #10
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: What is a Class Module

    Please Login or Register  to view this content.

  11. #11
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: What is a Class Module

    Oh, that's nothing to do with classes really, control just means any control - that's why you don't get intellisense, it just lets you loop through all controls on the userform whatever they may be

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Class Module...When and how.
    By Obfuscated in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-22-2014, 06:01 AM
  2. [SOLVED] Getting value from a class module
    By TankBuilder in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-12-2013, 05:33 PM
  3. [SOLVED] ComboBox class .AddItem filled in class module
    By Jacques Grobler in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-03-2012, 05:48 AM
  4. Replies: 1
    Last Post: 08-30-2011, 02:23 AM
  5. class module?
    By sybmathics in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 02-25-2006, 10:35 AM
  6. [SOLVED] class module
    By anonymousA in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2005, 07:08 AM
  7. [SOLVED] Class module
    By Avner Mediouni in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-19-2005, 11:06 AM

Tags for this Thread

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