StephanM
StephanM

Reputation: 753

Excel VBA function not working for Class

I have this function that is suppose to call its self for performing a summation. But when it gets to the line it pops up a window wanting a Macro. Does VBA not support this or am I missing something?

Sub SumUp(ByVal result As Double, ByVal number As Integer)

sumValue As Double
numValue As Integer

numValue = number

If (number > 0) Then

    sumValue = numValue * 2
    numValue = numValue - 1
    SumUp(result = sumValue, number = numValue)

Else
    MsgBox numValue
End If

End Sub

Upvotes: 0

Views: 113

Answers (2)

Toni Kanoni
Toni Kanoni

Reputation: 2317

1.) Technically it's not a function because it doesn't return any value, so the call SumUp(result = sumValue, number = numValue) is wrong

2.) You are missing Dim statements

-->

Sub SumUp(ByVal result As Double, ByVal number As Integer)

Debug.Print "result=" & result & ", number=" & number

Dim sumValue As Double
Dim numValue As Integer

numValue = number

If (number > 0) Then

    sumValue = numValue * 2
    numValue = numValue - 1


    SumUp sumValue, numValue

Else
    MsgBox numValue
End If

End Sub

Executing

Sub testit()
SumUp 2, 2
End Sub

delivers the following output:

result=2, number=2
result=4, number=1
result=2, number=0

Upvotes: 1

GSerg
GSerg

Reputation: 78165

SumUp(result = sumValue, number = numValue)

This is invalid on many levels.

If you meant to call SumUp with named parameters, you should have written

SumUp result:= sumValue, number:= numValue

You don't have to use named parameters though:

SumUp sumValue, numValue

Upvotes: 0

Related Questions