Function Procedure - Heeyoung-Ahn/Excel_VBA GitHub Wiki

Function Procedure

  • ํ”„๋กœ์‹œ์ € ๋‚ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜
    (์ฐธ์กฐ) Sub Procedure: ํ”„๋กœ์‹œ์ €์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰
  • ํ”„๋กœ์‹œ์ €์˜ ๊ตฌ๋ฌธ์— Function Procedure ์ด๋ฆ„์œผ๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ ํ•จ
  • Function Procedure๋Š” ์›Œํฌ์‹œํŠธ ๋˜๋Š” ๋‹ค๋ฅธ ํ”„๋กœ์‹œ์ €์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  • ์ƒ๋žต๊ฐ€๋Šฅํ•œ ์ธ์ˆ˜๋Š” Optional๋กœ ์„ ์–ธํ•˜๋ฉฐ ์ƒ๋žตํ•  ๊ฒฝ์šฐ์˜ ๊ธฐ๋ณธ๊ฐ’์„ ์ง€์ •ํ•ด์•ผ ํ•จ
  • ByVal vs. ByRef
  • Sample
Option Explicit

Function listCustomer(rngDB As Range, Optional lngAmt As Long = 100) As String
    Dim strCustomer As String
    Dim rng As Range
    
    Application.Volatile False
    For Each rng In rngDB
        If rng.Value >= lngAmt Then
            strCustomer = strCustomer & rng.Offset(0, -1).Value & ", "
        End If
    Next rng
    If Len(strCustomer) > 0 Then
        listCustomer = Left(strCustomer, Len(strCustomer) - 2)
    Else
        listCustomer = vbNullString
    End If    
End Function

Sub callFunctionTest(rngA As Range, lngA As Long)
    MsgBox listCustomer(rngA, lngA)    
End Sub

Sub callSubTest()
    Call callFunctionTest(Range(โ€œB2โ€, Cells(Rows.Count, 2).End(xlUp)), 100)    
End Sub