5

In a VBA custom class module (and by extension VB6?), if there's a private instance variable defined as a public enumeration from within the same class, should it be qualified..? If so, how..?

I often have project references with identical types. For instance, Excel and Word both have a Range object. As such, I'm always as specific as possible when declaring variables, such as Excel.Range or Word.Range, rather than just Dim R As Range.

But what about Enumerations..? How to be specific with these..? I've tried to qualify a variable as an enumeration, but I always get an error. I tried moving the enumeration definition to another custom class, but this didn't help. I don't often create classes for VBA, so I may be barking up the wrong tree with this.

If I qualify with the Me. keyword, I get the error:

"Compile error: Expected New or type name."

If I qualify with the custom class name, I get the error:

"Compile error: User-defined type not defined"

Here's a full example:

'Custom class module.
Option Explicit

Public Enum ImageAspect
    ImageAspectHorizontal
    ImageAspectVertical
End Enum

' Example 1:
' Qualified to use built-in Excel enumeration. This works.
Private logoAspect1 As Excel.XlOrientation 

' Example 2:
' Uses the enum defined in this class. This works, but...
' Can this be qualified..? How..? Is it even necessary?
Private logoAspect2 As ImageAspect

' Example 3:
' This does not work.
Private logoAspect3 As Me.ImageAspect

' Example 4:
' This does not work.
Private logoAspect4 As ThisClass.ImageAspect

Public Property Let Aspect(ByVal pAspect As ImageAspect)
    logoAspect2 = pAspect
End Property

Public Property Get Aspect() As ImageAspect
    Set Aspect = logoAspect2
End Property
7
  • 2
    Try YourProjectName.YourEnum Commented Jan 7, 2019 at 19:53
  • Yes, my apologies...unfortunately I neglected to add it to my example and say that it worked. But, the class I'm working on will be imported into a variety of other projects. Is there a more generic way to do it..? So I don't have to change the code every time I import it..? Commented Jan 7, 2019 at 20:26
  • 2
    If you include it as part of that namespace, I don't think you have much of a choice. Can you leave that class in a separate project? This might be helpful for your project, not sure. support.microsoft.com/en-ca/help/555159 Commented Jan 7, 2019 at 20:34
  • Ok thanks. I've tried code-library project references in both Access and Excel, and I'm not quite sold on the idea. Commented Jan 11, 2019 at 16:32
  • Maybe make the class part of an Add-in? I'm grasping at straws now though, haha. I'd be curious too see what you decide on :) Commented Jan 11, 2019 at 17:26

1 Answer 1

1

They can only be classified at the project level.

Public types and enums are declared at the project level. This means they can't be classified further than the project, and you can't have multiple types or enums with the same name in a single project.

Public Enum ImageAspect
    ImageAspectHorizontal
    ImageAspectVertical
End Enum

Private logoAspect4 As VBAProject.ImageAspect

VBAProject is the default name of an Excel VBA Project. It can be renamed in the VBE under Tools -> VBAProject Properties

This is independent on if they're stored in a class module or normal module. They're always declared at the project level. If you want re-usable code across projects, you can store your enums and types in a different file with a distinct project name and reference that file to import the types and enums.

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

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.