设置类对象数组属性

我试图设置一个对象的属性,这是一个类对象数组的一部分,对于Excel中的VBA。

代码如下所示:

Dim myClass(5) as class1 Dim i as integer For i = 0 to 5 set myClass(i) = New class myClass(i).myProperty = "SomeValue" Next i 

类代码简单地说就是:

 Private pmyProperty as string Public Property Let myProperty(s as string) pmyProperty = s End Property Public Property Get myProperty() as string myProperty = pmyProperty End Property 

但是,当我运行这个,我得到一个编译错误“预期:列表分隔符”。 这个命中myClass(i).myProperty =“SomeValue”行。

如何设置属于数组一部分的类对象的属性的值?

任何帮助将是伟大的!


所以实际的代码如下…

模块代码:

 Public Sub main_sb_BillingApp() Dim intCountComplete As Integer Dim intLastRow As Integer Dim Line() As clsLine Dim i As Integer, x As Integer intCountComplete = WorksheetFunction.CountIf(Sheets(WS_NAME).Columns(COL_W_COMPLETE), "Yes") intLastRow = Sheets(WS_NAME).Cells(LAST_ROW, COL_W_COMPLETE).End(xlUp).Row - 1 ReDim Line(intCountComplete - 1) For i = ROW_W_HEADER + 1 To intLastRow If Sheets(WS_NAME).Cells(i, COL_W_COMPLETE) = "Yes" Then Set Line(x) = New clsLine Line(x).Row = i x = x + 1 End If Next i End Sub 

class级代码:

 Private pDate As Date Private pUJN As String Private pDesc As String Private pCharge As Currency Private pCost As Currency Private pMargin As Double Private pComplete As Boolean Private pRow As Integer Public Property Let Row(i As Integer) pRow = i Update End Property Public Property Get Row() As Integer Row = pRow End Property Private Sub Update() With Sheets(WS_NAME) pDate = .Cells(pRow, COL_W_DATE) pUJN = .Cells(pRow, COL_W_UJN) pDesc = .Cells(pRow, COL_W_DESC) pCharge = .Cells(pRow, COL_W_CHARGE) pCost = .Cells(pRow, COL_W_COST) pMargin = .Cells(pRow, COL_W_MARGIN) If .Cells(pRow, COL_W_COMPLETE) = "Yes" Then pComplete = True Else pComplete = False End If End With End Sub 

Line是一个VBA保留关键字,所以你混淆了编译器。 改变你的对象数组的名称,它工作得很好:

 Dim lineArray() As clsLine '... Set lineArray(x) = New clsLine lineArray(x).Row = i