Does anyone use class modules?

Johnny C

Well-known Member
Joined
Nov 7, 2006
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
I've been coding VBA since (ahem) 1993 or thereabouts. When the VBA interface came in there was always this pesky option of a class module, I never knew what they did because I'd never needed to use them (and I've done some hefty modules in my time, 1,000+ rows of VBA)

I recently thought, really I should know about them. So I learnt how to use them. I've used them in projects, simply because I wanted to understand them and get familiar with them. However they still seem over the top, I use them to do things I could have done in half the time without using classes. Plus there's always the thing that not many people seem to know or use them (I rarely see posts on here about them) and as a contractor my code always has to be as simple as possible for when someone else picks it up after I leave.

I appreciate that programmatically they're a more elegant way of doing something, turning values in cells into real data items. I'd expect to be expected to use them if I was doing an academic VBA course for example. But in the real world, Occam's razor applies, you need something doing as quickly as possible and as simply as possible.

The sort of stuff I've used them for tends to be where there's large tables of data that needs feeding into multiple sheets, make the sheetname a member of the class then all the columns of data as separate member types, pop them into a collection, then loop through the collection, pluck the data items for each class and populate the sheet with them. Something I would do in 15-30 mins normally, it probably takes twice as long with a class module approach.

Do people use them much? Is there a time when they really should be used? is there a reason they should be used (other than they're more elegant)?
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I'm fairly new to VBA, but I do use class modules from time to time. TL;DR - I use them for manipulating/reading userForms

My CS background is very limited, but they did drive home the use of Object constructors and Setters/Getters, so maybe that influenced my decision to look into them?

I use custom class modules for two reasons:

1. To monitor changes between checkBoxes or optionButtons (on userForms) and modify the userForm according to currently selected options (via collection).

2. I build worksheets based on different userForm selections, and class modules were an easy was to "catalog" all the different form attributes.
This allows me to store important info like the space to allocate for different 'chunks' and a Boolean for whether or not to include the 'chunk'.

Here is an example of No.2

Code:
''=======================================================
'' Program:     clsModule
'' Desc:        Form Builder, Module Object Class
''              Tracks the required modules for given job
'' Called by:   *********
'' Call:        None
'' Arguments:   None
'' Comments:    (1) _Initialize - Sets usage to 'off'
''                  (off -> will not display on new form)
''              (2) _Initialize - Sets height to 1 to
''                  ensure a nonzero space allocation
''              (3) Object properties will be modified by
''                  product form generator (userForm)
'' Changes----------------------------------------------
'' Date         Programmer          Change
'' 3/13/18      *************       Created
''
''=======================================================

''========VARIABLE DECLARATION===========================
    
    'Number of Rows - Space to be allocated for module
    Public modHeight As Long
    
    'Toggle for usage/display for form builder
    Public modActive As Boolean
        
''=======================================================
'' Name:        Class_Initialize()
'' Purpose:     Constructor for objects used to track
''              Existence and Size of Modules to be
''              Placed on newly created form
'' Inputs:      None
'' Returns:     Constructor
''=======================================================

Private Sub Class_Initialize()

    modHeight = 1                   'Allocate height = 1 row
    modActive = False               'Disable Module

End Sub

I have a long list of stuff like this to allocate space (in another module)

Code:
        'Basic Info 
        Dim basicInfoMod As New clsModule
            basicInfoMod.modHeight = 9
            
        'Wood Info (Species, Grade, Thickness)
        Dim woodInfoMod As New clsModule
            woodInfoMod.modHeight = 3

And then, based on userForm selections I turn them On/Off by toggling the objects .modActive property.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top