컴퓨터/엑셀

vba 함수 만들기 여러 개 인수를 더하는 함수 만들기

풍경소리^^ 2024. 8. 15. 18:31

함수더하기여러인수.xls
0.04MB

Function plus(ParamArray args() As Variant) As Double
    Dim total As Double
    Dim arg As Variant
    Dim cell As Range
    
    total = 0
    
    For Each arg In args
        If TypeName(arg) = "Range" Then
            ' Range 객체인 경우, 범위 내의 모든 셀의 값을 더합니다.
            For Each cell In arg
                If IsNumeric(cell.Value) Then
                    total = total + cell.Value
                End If
            Next cell
        ElseIf IsNumeric(arg) Then
            ' 숫자 값인 경우, total에 더합니다.
            total = total + arg
        Else
            ' 숫자도 아니고 범위도 아닌 경우 오류 발생
            Err.Raise vbObjectError + 9999, , "Arguments must be numeric or range objects."
        End If
    Next arg
    
    plus = total
End Function