Adding or multiplying variants in VBA

后端 未结 2 1156
旧时难觅i
旧时难觅i 2020-12-11 12:15

Suppose we are given two variants, X and Y, that may be numbers, ranges or arrays. Is there a simple way to add or multiply them like in worksheet

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-11 13:14

    After looking at various options, i've settled on a Worksheetfunction method. The most viable candidates for arithmetic calculations appear to be in the financial category. From Excel help on the PV function when rate = 0, the following relation exists among the arguments: pmt * nper + pv + fv = 0. This relation also applies to each of the other corresponding functions. Therefore one option would be:

    Sub AddMult()
        Dim X, Y, Add, Mult
        X = Array(Array(1, 3), Array(2, 4))
        Y = Array(1, 2)
        With Application
            Add = .Pmt(, -1, X, Y)
            Mult = .PV(, 1, .PV(, X, Y))
        End With
    End Sub
    

    For other operations on variants, further WorksheetFunction methods are available:

    .SLN(x,y,1)     'x-y
    .SLN(x,,y)      'x/y
    .Power(x,y)     'x^y 
    .Quotient(x,y)  'x\y 
    .Delta(x,y)     'x=y
    .GeStep(x,y)    'x>=y
    

    Note: Prefix by Application (not Worksheetfunction which doesn't allow for other data types.)

提交回复
热议问题