Search code examples
excelexcel-2007declarationvba

Excel, Visual Basic Editor, Enum type declaration - User-defined type not defined


I have the following code and I don't know why Excel doesn't see the Enum type (that I've declared) in the Test2() subroutine:

Private Sub Test1()
    Dim test_name As Variant

End Sub


Private Enum rlcRollercoasterState
    rlcRollercoasterDisabled
    rlcRollercoasterEnabled
    rlcRollercoasterBroken
    rlcRollercoasterMissing
End Enum


Private Sub Test2()
    Dim var1 As Variant

    Dim rlcRC1State As rlcRollercoasterState ' User-defined type not defined

End Sub

If I copy the Test1() subroutine after the Enum type declaration, Excel recognizes the rlcRollercoasterState type in Test1(). If I comment out the Test1() subroutine, Excel recognizes rlcRollercoasterState type in Test2().

Why does the Excel fail to see the rlcRollercoasterState type in the Test2() subroutine?


Solution

  • enter image description here

    Always "Debug/Compile VBAProject" (Alt-dl) before running the code. As in your example it give the error. The compile error says it wants the declaration statement at the beginning of the module.