2017-08-07 108 views
2

我知道很多人都对这个错误提出了问题,但基于这些答案,我应该做的都是正确的。Excel VBA“类型不匹配:数组或预期的用户定义类型”

我创建了一个名为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 

这里是我的问题:当我运行它,它给了我一个编译器错误“类型不匹配:数组或用户定义类型预期”在units(atRow).setVariables (variables)字上variables

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

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

+0

这个'Private variables()As Variable'应该是'Private variables()As Variant'),并且需要在整个代码中进行相同的更改。除非您自己定义它,否则不存在“变量”类型。 – braX

+0

我创建了一个名为“变量”的类。这不足以将其定义为类型吗? –

+0

啊 - 你只是没有显示。我明白你现在在哪里提及它。没有人可以在没有定义的情况下测试任何代码。 – braX

回答

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 s,它可以保护您免受怪异的角落情况。应该进一步开发此代码,我会建议使用显式工作表变量,而不是依靠隐含的ActiveSheet。参见例如this answer

+0

非常感谢! –