【问题标题】:Setting Class Object Array Property设置类对象数组属性
【发布时间】:2017-02-20 15:32:37
【问题描述】:

我正在尝试为 VBA 中的 excel 设置作为类对象数组一部分的对象的属性。

代码如下:

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

【问题讨论】:

  • 是VB.NET or vba or vbscript?
  • 显示相关class代码
  • 我使用的代码是 VBA for excel。
  • 添加了班级代码。
  • 你的实际代码为我编译干净。

标签: arrays excel class object vba


【解决方案1】:

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

Dim lineArray() As clsLine
'...

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

【讨论】:

  • 改了名字,效果很好!感谢您的帮助!
猜你喜欢
  • 2016-10-27
  • 2013-10-03
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
  • 2016-07-06
相关资源
最近更新 更多