Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:是否可以使用自定义方法创建自定义函数?_Vba_Function_Excel_Methods - Fatal编程技术网

Excel VBA:是否可以使用自定义方法创建自定义函数?

Excel VBA:是否可以使用自定义方法创建自定义函数?,vba,function,excel,methods,Vba,Function,Excel,Methods,我指的是,例如,你如何做到: Range().Select 其中“Range()”是函数,“Select”是方法 例如,如果我有一个函数,我想取三个表示三角形边长的双倍,然后以任意一个弧度的度数吐出最大的角度 Public Function getAngle(a as Double, b as Double, c as Double) .degrees = 'some equation to determine degrees as a double .rads = 'som

我指的是,例如,你如何做到:

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

例如,如果我有一个函数,我想取三个表示三角形边长的双倍,然后以任意一个弧度的度数吐出最大的角度

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
使用类型:

Option Explicit

Type cType
    Degrees As Double
    rads As Double
End Type


Sub tester()

    Dim a As cType

    a = getAngle(1, 2, 3)

    Debug.Print a.Degrees, a.rads

End Sub


Public Function getAngle(a As Double, b As Double, c As Double) As cType
Dim rv As cType

    rv.Degrees = 88  'for example
    rv.rads = 99     'for example

    getAngle = rv

End Function

您可以使用自定义类型或类并从函数返回该类型或类。