Excel VBA"类型不匹配:预期的数组或用户定义类型"

时间:2017-08-07 17:05:34

标签: arrays excel vba user-defined-types type-mismatch

我知道很多人都对这个错误提出了问题,但根据这些错误的答案,我应该做对了。

我创建了一个名为Variable的类来存储有关变量的多条信息。我有另一个名为Equipment的类,它存储了这些变量的数组。以下是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

这是我的问题:当我运行程序时,它会给我一个编译器错误&#34;类型不匹配:数组或用户定义的类型预期&#34;关于variables中的units(atRow).setVariables (variables)字。

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

谢谢!我真的很感激帮助!!

1 个答案:

答案 0 :(得分:1)

你有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。是的,这是在每个模块和每个类模块中。否则就是抛弃编译器的调试帮助。

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

修改我添加了一些CStr,可以保护您免受奇怪的角落攻击。应该进一步开发此代码,我建议使用显式工作表变量而不是依赖隐式ActiveSheet。例如,参见this answer