设置类对象数组属性

时间:2017-02-20 15:32:37

标签: arrays excel excel-vba class object vba

我试图在VBA中为excel设置属于类对象数组的对象的属性。

代码如下所示:

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

类代码很简单:

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

然而,当我运行它时,我得到一个编译错误" expected:list separator。"这击中了myClass(i).myProperty =" SomeValue"线。

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

任何帮助都会很棒!

所以实际代码如下......

模块代码:

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

班级代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapFragmentContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" />

1 个答案:

答案 0 :(得分:4)

Line是一个VBA保留关键字,因此您会混淆编译器。更改对象数组的名称,它可以正常工作:

Dim lineArray() As clsLine
'...

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