VBA 运行时错误 438:对象不支持此属性或方法



I get

错误 438

尝试将元素添加到变体数组时。你能帮我调试吗?感谢

Public Function CouponList() As Double
    Dim nbCoupons_lg As Integer
    Dim counter_lg As Integer
    Dim coupons_var As Variant
    Dim coupon As Cls_Coupon
    nbCoupons_lg = Maturity_db * CouponPeriodicity_db
    If (Not nbCoupons_lg = 0) Then
        ReDim coupons_var(1 To nbCoupons_lg) As Variant
        For counter_lg = 1 To nbCoupons_lg
            Set coupon = New Cls_Coupon
            coupon.Period_lg = counter_lg
            coupon.Value_db = AnnualCouponRate_db * ParValue_db
            coupon.PresentValue_db = coupon.Value_db / (1 + AnnualDiscountRate_db) ^ (coupon.Period_lg / Maturity_db)
            coupons_var(counter_lg) = coupon
        Next counter_lg
    End If
    CouponList = coupons_var
End Function

想象一下,你有一个类Party(就像你的Coupon一样),如下所示:

Private m_lGuestsNumber As Long
Public Property Get GuestsNumber() As Long
    GuestsNumber = m_lGuestsNumber
End Property
Public Property Let GuestsNumber(ByVal lNewValue As Long)
    m_lGuestsNumber = lNewValue
End Property

如果你想拥有不同的 Party 类型的对象,通过循环放入数组中,这是一个很好的方法:

Public Sub TestMe()
    Dim myArr()         As Variant
    Dim cnt             As Long
    Dim additional      As Long: additional = 10
    Dim coupon          As Party
    ReDim myArr(1 To additional)
    For cnt = 1 To additional
        Set coupon = New Party
        coupon.GuestsNumber = cnt * 2
        Set myArr(cnt) = coupon
    Next cnt
End Sub

现在,您可以轻松地将上述内容与您的代码交换。它应该在工作。

相关内容

  • 没有找到相关文章

最新更新