简体   繁体   English

数组 VBA 中的用户定义对象

[英]User-Defined Object In Array VBA

As the subject line states, I am trying to store objects of a class I wrote into an Array.正如主题行所述,我试图存储我写入数组的类的对象。 VBA is giving me the frustrating error: VBA 给了我令人沮丧的错误:

'Object Variable or With Block variable not set' '对象变量或未设置块变量'

I'm pretty sure I am defining my object correctly, and the local variables show that the object is of my defined type and all of it's fields are filled in, so I can't figure out where my issue is.我很确定我正确定义了我的对象,并且局部变量显示该对象是我定义的类型并且它的所有字段都已填写,所以我无法弄清楚我的问题出在哪里。

Dim Type1(2 To 250) As myClass
Dim Type2(2 To 250) As myClass
Dim Type3(2 To 250) As myClass
Dim temp_obj As myClass
Dim foo As String

For i = 2 To 250
    Set temp_obj = New myClass
    With temp_obj
        .field1 = Worksheets("Sheet1").Rows(i).Columns(2).Value
        .field2 = Worksheets("Sheet1").Rows(i).Columns(3).Value
        .field3 = Worksheets("sheet1").Rows(i).Columns(4).Value
        .field4 = Worksheets("Sheet1").Rows(i).Columns(5).Value
    End With
    foo = Worksheets("Sheet1").Rows(i).Columns(1).Value
    If foo = "Type1" Then
        Type1(i) = temp_obj
    ElseIf foo = "Type2" Then
        Type2(i) = temp_obj
    ElseIf foo = "Type3" Then
        Type3(i) = temp_obj
    End If
Next i

My class looks something like this:我的班级看起来像这样:

Private pfield1 As Single
Private pfield2 As Integer
Private pfield3 As String
Private pfield4 As String

Public Property Get field1() As Single
    field1 = pfield1 
End Property

Public Property Get field2() As Integer
    field2 = pfield2
End Property

Public Property Get field3() As String
    field3 = pfield3
End Property

Public Property Get field4() As String
    field4 = pfield4
End Property

Public Property Let field1(p As Single)
    pfield1 = p
End Property

Public Property Let field2(p As Integer)
    pfield2 = p
End Property

Public Property Let field3(p As String)
    pfield3 = p
End Property

Public Property Let field4(p As String)
    pfield4 = p
End Property

Try this change at the bottom:在底部尝试此更改:

If foo = "Type1" Then
    Set Type1(i) = temp_obj
ElseIf foo = "Type2" Then
    Set Type2(i) = temp_obj
ElseIf foo = "Type3" Then
    Set Type3(i) = temp_obj
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建用户定义对象类型的数组 - Create an array of user-defined object type 存储在数组中时包含指针中断的用户定义 object - User-defined object containing pointer breaks when stored in array VBA函数似乎返回一个数组,但我收到“类型不匹配:预期为数组或用户定义类型” - VBA function seems to return an array but I get “Type mismatch: array or user-defined type expected” VBA子调用给出“编译错误:类型不匹配:数组或用户定义的类型预期” - VBA sub call gives “compile error: Type mismatch: array or user-defined type expected” MS Access VBA:无法将数组变量传输到用户定义的函数 - MS Access VBA: Can not transfer array variables to user-defined function C ++中用户定义数组的问题 - Issue with user-defined array in C++ 在QBasic中的用户定义的TYPE中使用数组 - Use an array in a user-defined TYPE in QBasic 具有用户定义维数的大型数组 - Large Array with user-defined number of dimensions 用户定义类型和将数组传递给函数 - User-defined Type & passing Array to Function 是否可以将Java基本类型和引用对象类型转换为与用户定义对象相同的字节数组? - Is it possible to convert java primitive and reference object types to a byte array as same as user-defined objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM