Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 初始化N类对象;VBA_Excel_Vba_Class_Loops_Object - Fatal编程技术网

Excel 初始化N类对象;VBA

Excel 初始化N类对象;VBA,excel,vba,class,loops,object,Excel,Vba,Class,Loops,Object,在ExcelVBA中工作,我试图初始化“CUserType”类的几个对象的数组,但遇到了一些问题。我试着在一个循环中这样做: For count = 1 To size Dim myArray(count) As CUserType Next 但是,VBA似乎希望数组索引为常量整数 现在我尝试用一个单独的函数来实现它,如下所示: Sub ititUsers(num As Integer) Dim myArray() As CUserType Redim myA

在ExcelVBA中工作,我试图初始化“CUserType”类的几个对象的数组,但遇到了一些问题。我试着在一个循环中这样做:

For count = 1 To size
    Dim myArray(count)   As CUserType
Next
但是,VBA似乎希望数组索引为常量整数

现在我尝试用一个单独的函数来实现它,如下所示:

Sub ititUsers(num As Integer)

Dim myArray()           As CUserType
Redim myArray(1 to num)

If num = 1 Then
    Dim myArray(1)      As CUserType
ElseIf num = 2 Then
    Dim myArray(1)      As New CUserType
    Dim myArray(2)      As New CUserType
ElseIf num = 3 Then
    Dim myArray(1)      As New CUserType
    Dim myArray(2)      As New CUserType
    Dim myArray(3)      As New CUserType
.
.
.
End Sub
当然,这既单调又浪费。我是新手,所以我知道我可能错过了一些东西。有什么帮助吗


谢谢大家!

您的行
Dim myArray(count)As CUserType
已将数组的所有元素设置为CUserType。如果只需要对其进行初始化,请执行For循环:

Sub ititUsers(num As Integer)

    Dim i As Long
    Dim myArray() As CUserType
    Redim myArray(1 to num)

    For i = 1 to num
        Set myArray(i) = New CUserType
    Next i

End Sub

您的行
Dim myArray(count)As CUserType
已将数组的所有元素设置为CUserType。如果只需要对其进行初始化,请执行For循环:

Sub ititUsers(num As Integer)

    Dim i As Long
    Dim myArray() As CUserType
    Redim myArray(1 to num)

    For i = 1 to num
        Set myArray(i) = New CUserType
    Next i

End Sub