2016-08-26 1 views
2

내가, 예를 들어, 당신이 할 수있는 방법을 말하는 겁니다 : Excel VBA : 사용자 지정 메서드로 사용자 지정 함수를 만들 수 있습니까?

Range().Select 

경우 "범위()"기능, 그리고 "를 선택하면"방법입니다.

예를 들어 삼각형의 변의 길이를 나타내는 3 개의 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) .degrees : 90.0

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

+0

당신은 사용자 정의 유형 또는 클래스를 사용하고 있음을 반환 할 수 있습니다 귀하의 기능에서. –

답변

4

클래스를 만듭니다 (이 예제에서는 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 
1

타입 사용 :

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 
관련 문제