2017-02-20 152 views
0

我想設置一個對象的屬性,它是類對象數組的一部分,用於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 

類代碼:

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 
+2

它是VB.NET *或* vba *或* vbscript? – Plutonix

+0

顯示相關的'class'代碼 – user3598756

+0

我使用的代碼是excel的VBA。 – Nicolas

回答

4

Line是VBA保留關鍵字,所以你令編譯器感到困惑。更改您的對象數組的名稱,它的工作原理:

Dim lineArray() As clsLine 
'... 

     Set lineArray(x) = New clsLine 
     lineArray(x).Row = i  
+0

更改名稱並完美工作!謝謝你的幫助! – Nicolas