Josh
Josh

Reputation: 4632

VBA Option Private Module equivalent, but just for a single function in a Class Module?

In VBA, I know that if you want subs/functions to be accessible only within the current Project, you can put those methods into a module marked Option Private Module.

But what if I want to do this with a certain method within a Class Module? Can I separate out a single method from a Class?

Specifically, I have a Property in one of my Classes that I want to be Read-Only unless it is being used within the current Project. There are other Properties/Subs/Functions within that Class that I want to be truly Public.

I thought Friend Property Let... might be the correct way to do this, but when I try it, it doesn't stop another Project in the same open Excel session from assigning to the property. So I am looking for the equivalent of Option Private Module for a subset of functions within a Class.

Is this possible?


Another way of approaching this question would be: Can you spread a Class Module over multiple sub-modules?

Upvotes: 2

Views: 3472

Answers (1)

Josh
Josh

Reputation: 4632

Since there haven't been many eyeballs on this question, and I may not have even phrased it well, I just thought I would post how I decided to solve this issue in the meantime, for future generations.

Classes in VBA can only be Private or PublicNotCreateable, so I was already using a factory method to be able to create the object.
I just added some code to the factory method to assign a unique identifier to each object, and keep track of project-scope pseudo-properties in my own separate data structure. The Class has a "Get" for the property, but to assign to it, a special project-scope function in an Option Private Module Module needs to be called.
Not a perfect solution, but at least it does what I want.

Upvotes: 3

Related Questions