1

I am working on a rather large Excel application via VBA. I want to place all my public variables in one module. Then I would assign and use those variables in my other modules in the same workbook. Here's what I have.

Option Explicit

Public wdApp As Word.Application
Public wdDoc As Word.Document

But when I tried to assign those variables in my other modules, I got the following message. Shouldn't public variables be accessible to all modules? What am I missing?

enter image description here

2 Answers 2

1

You're missing the concept of objects, which are instances of a class module.

I'm betting my shirt that you've written that code in a class module (note, ThisWorkbook and Sheet42 modules are class modules).

Add a new standard module (.bas), and move your global/public variables there.

In order to access the public members of a class module, you need to New up an instance of that class first.

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

1 Comment

You're right. I had it as class module. A slip of my hand.
1

The declarations need to be in a standard module, not in any of the worksheet/workbook/class modules, otherwise they are treated as public properties of a class (namely the worksheet, workbook, or class).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.