本文介绍了在VBA中添加或增加变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们给出了两个变体: X Y ,可能是数字,范围或数组。
是否有一种简单的方法来添加或乘法,如工作表公式 = X + Y = X * Y



我想到的一种可能是使用Evaluate操作,如下所示:

  Dim X,Y 

Sub AddMult()
Dim Add,Mult
X = Array(Array(1,3),Array (2,4))
Y = Array(1,2)
Add = [GetX()+ GetY()]
Mult = [GetX()* GetY()]
End Sub
函数GetX()
GetX = X
结束函数
函数GetY()
GetY = Y
结束函数

似乎有点尴尬。任何其他想法?



(这是一个相关的问题:。

解决方案

查看各种选项后,我已经解决了一个Worksheetfunction方法。最可行的算术计算候选者似乎属于财务类别。当 rate = 0时,从 PV 函数的Excel帮助中,参数之间存在以下关系: pmt * nper + pv + fv = 0 。这种关系也适用于每个其他相应的功能。因此,一个选项是:

  Sub AddMult()
Dim X,Y,Add,Mult
X = Array(Array(1,3),Array(2,4))
Y = Array(1,2)
应用程序
Add = .Pmt(,-1,X ,Y)
Mult = .PV(,1,.PV(,X,Y))
结束
End Sub

对于变体的其他操作,可以使用进一步的WorksheetFunction方法:

  .SLN(x,y,1)'xy 
.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

注意:前缀by 应用程序 / em> Worksheetfunction 不允许使用其他数据类型。)


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 formulas =X+Y and =X*Y?

One possibility i thought of would be to use the Evaluate operation, something like this:

Dim X, Y

Sub AddMult()
    Dim Add, Mult
    X = Array(Array(1, 3), Array(2, 4))
    Y = Array(1, 2)
    Add = [GetX()+GetY()]
    Mult = [GetX()*GetY()]
End Sub
Function GetX()
    GetX = X
End Function
Function GetY()
    GetY = Y
End Function

It seems a little awkward though. Any other ideas?

(Here is a related question: Multiplying arrays with scalars and adding in VBA.)

解决方案

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.)

这篇关于在VBA中添加或增加变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 09:27