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.
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:
"The enumeration {0} declared within {1} should be declared within a standard module", {0} = enumeration identifier, {1} declaring Worksheet.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.
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.