1

I've created a VBA Class module and written all the necessary code within it. When I go to use it, in Excel, I instantiate it in a global module as follows.

Public gblFrequencyList As New clsLists

Then, in the Workbook module, I try to use it as follows.

Private Sub Workbook_Open()
    ...snip...
    gblFrequencyList.fromString "Once,Interval,Weekly,Monthly,Yearly"
End Sub

However, this returns the following error message when I open the workbook.

Type mismatch.

In Debug mode, when I place a Watch on the object, I get this:

Object variable or With block variable not set

Why? I know that Excel recognizes my Class module because I get this prompt when typing a new statement.

Excel VBA Prompt

Is there something different I need to do to get a global instantiation of my Class?

5
  • 6
    That works for me as long as the global declaration is in a regular module. Maybe the error is inside your class object? Is your debugging option set to "break in class module" ? Commented Nov 7 at 19:41
  • Thanks. I figured "Break on unhandled errors" covers all bases. But, apparently not. Commented Nov 7 at 19:52
  • So there was an issue inside the class? Commented Nov 7 at 20:01
  • Yes, there was. ;-) Commented Nov 7 at 20:09
  • @TimWilliams post an answer to that affect so I can accept it to close this question. Thanks. Commented Nov 7 at 23:55

2 Answers 2

3

That works for me as long as the global declaration is in a regular module.

Maybe the error is inside your class object? If your debugging option (Tools >> Options >> General >> Error Trapping) isn't set to "break in class module" then that can hide errors inside of your class object.

If you don't see a "Debug" button when you get a run-time error then that's a good sign you might need to change your error trapping

Sign up to request clarification or add additional context in comments.

Comments

1

For such case, you typically would not instanciate a global object yourself but leave it to the VBA compiler instead in the very same fashion VBA creates a ThisWorkbook object (unless you rename it) automatically.

You get 2 useful features if you do it this way:

  1. The object is created automatically and available in all the modules.
  2. VBA will recreate the object for you in case an exception is raised and its execution is stopped (i.e. no need to manually re-run the Workbook_Open Sub or check multiple time if the object still exists).

Here is how you do it:

  1. Copy the following into a file (the file name does not matter). The content of that file is what you'd get if you were to create a Class module and export it, then changed the line Attribute VB_PredeclaredId = False to True.
    VERSION 1.0 CLASS
    BEGIN
      MultiUse = -1  'True
    END
    Attribute VB_Name = "gblFrequencyList"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    
    Public Sub HelloWorld()
        MsgBox "Hello world"
    End Sub 
    
    For illustration I included a HelloWorld sub that you can call, but you will have to put your own sub/functions instead.
  2. Save the file and import it into your VBA project, it will create the gblFrequencyList class module and corresponding object.
    The VBA editor hides the attributes, only letting you edit the variables / Sub / Functions / Events / ... inside the class.
  3. Anywhere in your project, you can now reference the object, like so: The global object is automatically known by VBA

4 Comments

That is truly useful information (though manually creating the Class module, in that manner, is not necessary; because, the VBA Editor has a Properties window in which it exposes those attributes for ease of manipulation). Thanks.
Sorry... I just checked and, although there is a properties window, the particular attribute you mention is NOT exposed in that window. So, I just have to export my class module, manually change that attribute, and then reimport it to accomplish what you are pointing out. Thanks.
TBH, I believe you also have to edit the file outside the VBA editor to enable the For Each syntax on lists properly so I thought you wouldn't be surprised having to export the module. Same goes with module / function documentation BTW, this is only accessible after exporting the module to my knowledge.
Thanks for the info. I use comments inside each method from within the VBA Editor. I'm not sure of the type of documentation to which you refer. I am also not using FOR EACH in any of the methods in my class object. I use only InStr, Split, and Join. A simple call for example: If Not DelimitedLists.InList("Weekly,Monthly", Target.Value) Then

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.