Excel VBA "Type mismatch:array or user-defined type expected"

Excel VBA "Type mismatch:array or user-defined type expected"

我知道很多人都问过关于这个错误的问题,但根据对这些问题的回答,我应该做的一切都是正确的。

我创建了一个名为 Variable 的 class 来存储关于一个变量的多条信息。我有另一个名为 Equipment 的 class,它存储这些变量的数组。这是Equipment中的相关代码:

Public name As String
Private variables() As Variable

Public Sub setVariables(vars() As Variable)
    variables = vars
End Sub

我还有一个创建 Equipment 实例的模块。这是所有代码:

Public Sub fillEquipment()

    'figure out how many units of equipment there are
    numUnits = 0
    atRow = 1
    Do Until Range("A" & atRow).value = ""
        numUnits = numUnits + 1
        atRow = atRow + 1
    Loop

    'create array for equipment units
    Dim units() As Equipment
    ReDim units(0 To numUnits)

    'figure out how many variables there are
    numVars = 0
    For Each col In Range("A1:ZZ1")
        If col.value <> "" Then
            numVars = numVars + 1
        End If
    Next col

    'create an array of equipment one row at a time
    atRow = 1
    Do Until Range("A" & atRow).value = ""
        'create and name equipment
        units(atRow) = New Equipment
        units(atRow).name = Range("A" & atRow).value

        'create an array of vars
        Dim variables() As Variable
        ReDim variables(0 To numVars)
        For atCol = 1 To numVars
            variables(atCol) = New Variable
            variables(atCol).name = Cells(1, atCol).value
            variables(atCol).value = Cells(atRow, atCol).value
        Next atCol

        'add variables to equipment
        units(atRow).setVariables (variables)
        atRow = atRow + 1

    Loop

    'print for testing
    For atRow = 1 To numUnits
        Cells(atRow, 1).value = Equipment(atRow).name
        For atCol = 1 To numCols
            Cells(atRow, atCol + 1).value = Equipment(atRow).getVariables(atCol)
        Next atCol
    Next atRow

End Sub

这是我的问题:当我 运行 程序时,它在 units(atRow).setVariables (variables).

中的单词 variables 上给我一个编译器错误 "Type mismatch:array or user-defined type expected"

我不明白我做错了什么。 variables 被定义为对象类型 Variable 的数组,这正是 setVariables 所要求的。

谢谢!非常感谢您的帮助!!

你有 extra parentheses。这编译没有错误:

Sub make(numUnits As Long, numVars As Long)
    Dim units() As Equipment
    ReDim units(0 To numUnits)
    Dim atRow As Long, atCol As Long    ' <-- new Dim, because of Option Explicit

    'create an array of equipment one row at a time
    atRow = 1
    Do Until Range("A" & atRow).value = ""
        'create and name equipment
        units(atRow) = New Equipment
        units(atRow).name = CStr(Range("A" & CStr(atRow)).value)   ' <-- use CStr() anytime you need a string

        'create an array of vars
        Dim variables() As Variable
        ReDim variables(0 To numVars)
        For atCol = 1 To numVars
            variables(atCol) = New Variable
            variables(atCol).name = Cells(1, atCol).value
            variables(atCol).value = Cells(atRow, atCol).value
        Next atCol

        'add variables to equipment
        units(atRow).setVariables variables
        atRow = atRow + 1       ' ^^^^^^^^^ not (variables) - no parens

    Loop
End Sub

关键问题是括号。但是,这也会为您的变量添加 Dim 语句。正如@BruceWayne 所说,您应该始终 使用Option Explicit。是的,在每个模块和每个 class 模块中。否则就是放弃编译器的调试帮助。

我实际上也在每个模块的顶部使用 Option Base 0,主要是为了提醒自己我正在使用哪个系统:)。

编辑 我添加了一些 CStrs,它可以保护您免受奇怪的极端情况的影响。如果进一步开发此代码,我建议使用显式工作表变量而不是依赖隐式 ActiveSheet。参见,例如,this answer.