首页 > 解决方案 > 从集合中的对象获取属性(VBA)

问题描述

我创建了一个自定义类对象的集合,我能够检索除数量属性(这是一个数组)之外的所有对象属性

以下是我的代码

Sub Ledger()

Dim ActPeriod As Long
Dim ForcastPeriod As Long
Dim sth As Worksheet
Dim Account As New ClsAccount
Dim allaccounts As New Collection
ActPeriod = 3
ForecastPeriod = 3

For i = 1 To Sheet1.Range("A4:A26").count
If Sheet1.Cells(i, 1) <> 0 Then
counter = counter + 1
Set Account = New ClsAccount
With Account
     .Code = Sheet1.Cells(i, 1)
     .Name = Sheet1.Cells(i, 2)
     .amount = Sheet1.Range(Cells(i, 3), Cells(i, 2 + ActPeriod))
     allaccounts.add Account, .Code
End With
End If
Next i

MsgBox allaccounts(3).amount(1, 1)


End Sub

我用来创建类的代码如下

Private AccAmount As Variant
Private AccGrowth As Variant
Private AccName As String
Private AccCode As String


Property Let amount(amt As Variant)
   AccAmount = amt
End Property

Property Get amount() As Variant
   amount = AccAmount
End Property

Property Let Name(n As String)
   AccName = n
End Property

Property Get Name() As String
   Name = AccName
End Property


Property Let Code(c As String)
   AccCode = c
End Property

Property Get Code() As String
   Code = AccCode
End Property

我收到此错误

在此处输入图像描述

标签: vba

解决方案


MsgBox allaccounts(3).amount()(1, 1)

没有括号,VBA 认为您正在尝试传递1, 1给 Property Get 过程,并且没有使用任何参数定义...


推荐阅读