Rubberduck: Declaring Enum within worksheet raises inspection

Created on 27 Oct 2020  路  4Comments  路  Source: rubberduck-vba/Rubberduck

What
When an enum is declared within a worksheet. This can cause an Ambiguous name error if/when the worksheet is copied. I categorize this under Code Quality.

Why
When an enum is declared within a worksheet object and that worksheet is copied the enumeration is copied as well. This leads to an identical enum in two locations resulting in an Ambiguous name error.

Example
This code should trigger the inspection:

'Defined in a Worksheet
Public Enum Sheet1Enumeration
    Option1 = 0
    Option2 = 1
    Option3 = 3
    Option4 = 7
End Enum

'-----

'Defined in Standard Module
Public Sub GeneratesAnErrorOnceWorksheetIsCopied()
    Dim userVariable As Sheet1Enumeration
    userVariable = Option1
End Sub

QuickFixes
If a QuicxFix is allowed it should create a Standard Module and move the enumeration into that standard module.

  1. QuickFix Name: MoveDeclaredEnumerationFromWorksheetIntoStandardModule

    Example code, after quickfix is applied:

    ```vb
    'Worksheet
    'No longer contains enumeration. Any other members (Subs, Functions, Constants, etc...) are unaffected

    '-----

    'Defined in a Standard Module (Named EnumerationDeclarations)
    Public Enum Sheet1Enumeration
    Option1 = 0
    Option2 = 1
    Option3 = 3
    Option4 = 7
    End Enum

    ```

Resources
Each inspection needs a number of resource strings - please provide a suggestion here:

  • InspectionNames: Enumeration defined within Worksheet object.
  • InspectionInfo: Copying a worksheet within which an enumeration is declared may lead to ambiguous name errors.
  • InspectionResults: "The enumeration {0} declared within {1} should be declared within a standard module", {0} = enumeration identifier, {1} declaring Worksheet.
enhancement feature-inspections up-for-grabs

Most helpful comment

@BZngr This issue does not apply to public user defined types, since they are not legal in document modules, or any kind of class module.

All 4 comments

The same issue would apply to Public UserDefinedType declarations in a worksheet.

This would be also true for any kind of class modules, not just the worksheet or any other document module, no?

Correct. I bring this up since Worksheet modules can be copied without ever navigating to the VBIDE. This makes for very confused users when they never touched any code. What was previously working functionality attached to buttons on a Worksheet now barfs because compilation can't occur.

@BZngr This issue does not apply to public user defined types, since they are not legal in document modules, or any kind of class module.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Gener4tor picture Gener4tor  路  3Comments

retailcoder picture retailcoder  路  3Comments

SteGriff picture SteGriff  路  3Comments

retailcoder picture retailcoder  路  3Comments

eteichm picture eteichm  路  4Comments