Excel 初始化N类对象;VBA
在ExcelVBA中工作,我试图初始化“CUserType”类的几个对象的数组,但遇到了一些问题。我试着在一个循环中这样做: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
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