首页 > 解决方案 > VBA 编译器在定义类、全局变量和子函数或函数时的事件顺序是什么?

问题描述

我在声明级别的代码中破坏了全局 Enum 类型声明,让我认为我错过了一个包含许多类和函数的大型项目中的结束标记。希望将我的搜索范围缩小到编译器中全局声明之前的任何内容。错误是“需要常量表达式”

枚举是金融模拟程序中使用的三个枚举之一。我正在调试一个我修复的类,然后所有的枚举都坏了。用常量修复第一次调用只会导致下一次调用出错。

我做了一个测试子来隔离调用,没有函数调用,但枚举仍然无法识别。

全球宣言:

Public Enum DistributeType
    dSalary = 0
    dUnit = 1
    dShares = 2
End Enum

Public Enum OptionType
    oNew = 0
    oRecycle = 1
End Enum

Public Enum ColumnType
    ctName = 1      'A
    ctBirth         'B
    ctHire          'C
    ctParticipate   'D
    ctSex           'E
    ctSalary        'F
    ctHours         'G
    ctTermination   'H
    ctStatus        'I
    ctNonVested     'J
    ctCashAccount   'K
    ctLStockAccount 'L
    ctNStockAccount 'M
    ctStockDivest   'N

End Enum

Sub Test()
    Dim i As Integer

    i = DistributeType.dSalary

    Debug.Print i

End Sub

我在做什么:

Initialialize_PayOut()
With employee
    'Forfeiture Delay
        Dim i As Integer
        i = 0
        i = Range("v_DelayForf").value
        .ForfeitDelay = i

        Debug.Print "Forfeit Delay: " & .ForfeitDelay
End with
End Sub

编辑类员工:

Private pForfeitDelay As Integer

Public Property Get ForfeitDelay() As Integer
    ForfeitDelay = pForfeitDelay
End Property

Public Property Let ForfeitDelay(value As Integer)
    pForfeitDelay = value
End Property

这段代码似乎没有任何问题,但是如果不复制整个项目,就不知道它可能在哪里。

标签: excelvbaglobal-variables

解决方案


虽然不是对实际问题的回答,但 Alex K. 提供了代码无法正常工作的解决方案。

枚举解析存在一个古老的错误,可能会导致该错误,可以通过删除其中一个值、单击块然后撤消或重新键入它(或简单地重新启动 Excel)来修复 - Alex K.


推荐阅读