Felipe
Felipe

Reputation: 3

VBA Runtime error 438: object doesn't support this property or method

I get

error 438

when trying to add an element into a variant array. Can you help me debugging, pls ? Thx

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

Upvotes: 0

Views: 1318

Answers (1)

Vityata
Vityata

Reputation: 43595

Imagine you have a class Party (like yours Coupon) like this:

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

If you want to have different objects of type Party, put into an array through a loop, this is a good way to do it:

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

Now you can easily exchange the above with your code. It should be working.

Upvotes: 2

Related Questions