首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Excel VBA“类型不匹配:预期数组或用户定义类型”

Excel VBA“类型不匹配:预期数组或用户定义类型”
EN

Stack Overflow用户
提问于 2017-08-07 17:05:34
回答 1查看 907关注 0票数 2

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

我创建了一个名为Variable的类来存储有关变量的多个信息片段。我有另一个名为Equipment的类,它存储这些变量的数组。以下是Equipment中的相关代码

代码语言:javascript
运行
复制
Public name As String
Private variables() As Variable

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

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

代码语言:javascript
运行
复制
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

我的问题是:当我运行这个程序时,它会在variables ( units(atRow).setVariables (variables) )一词上给出一个编译器错误“Type失配:数组或用户定义的类型”。

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

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

EN

Stack Overflow用户

回答已采纳

发布于 2017-08-07 18:05:38

你有额外括号。这在编译过程中没有错误:

代码语言:javascript
运行
复制
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。参见,例如,这个答案

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45552172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档