vba - Excel VBA : Is it possible create custom functions with custom methods?

标签 vba function excel methods

我指的是,例如,你可以如何做:

Range().Select

其中“Range()”是函数,“Select”是方法。

例如,如果我有一个函数,我想采用三个代表三角形边长的 double ,并以任一弧度表示最大角度。

Public Function getAngle(a as Double, b as Double, c as Double)

    .degrees = 'some equation to determine degrees as a double
    .rads = 'some equation to determine radians as a string

End Function

因此,您将得到以下结果:

getAngle(3, 4, 5).度:90.0

getAngle(3, 4, 5).rads : "0.5π"

最佳答案

创建一个类,在此示例中将其命名为 clsTrig。

Option Explicit

'/ Class  Name : clsTrig

Private m_ddegrees   As Double
Private m_drads  As Double

Public Property Get degrees() As Double
    degrees = m_ddegrees
End Property

Public Property Let degrees(val As Double)
     m_ddegrees = val
End Property

Public Property Get rads() As Double
    rads = m_drads
End Property

Public Property Let rads(val As Double)
     m_drads = val
End Property


Public Function doCalc(a1 As Double, a2 As Double) As Double

    '/ You do the math here. This is just a sample and not actual trigonometery

    m_ddegrees = a1 + a2
    m_drads = a1 - a2


End Function

然后在标准模块中,您将获得所需的行为,如下所示:

Public Function getAngle(a As Double, b As Double) As clsTrig
    Dim oTrig As New clsTrig

    Call oTrig.doCalc(a, b)
    Set getAngle = oTrig
End Function

Sub test()
    MsgBox getAngle(30, 20).degrees
    MsgBox getAngle(30, 20).rads
End Sub

关于vba - Excel VBA : Is it possible create custom functions with custom methods?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39171227/

相关文章:

Excel 给出错误的平均值

正则表达式 - URL 正则表达式专门用于 Excel VBA

javascript - Outlook 2010 电子邮件到网页,包括图像

javascript - 在javascript函数中自定义html

php生成excel并下载

excel - 将所有突出显示的单元格从一张纸复制到另一张纸

vba - 保存事件后触发 MS Word 宏

excel - excel可以清理自由文本用户输入吗?

javascript - 关于 "var avg = array.average()"中 array.average() 功能的混淆

angular - 如何从 typescript 中另一个函数的外部访问函数内部的变量?