1

Hi stackoverflow community, I have issue with the following code, I'd like to create a Function that if "CONDITION" is true then sum the selected cells and if not subtract.

 Function balance(condition As Double, amount As Integer, residue As Integer)
 
     If condition = "PAGO" Or "PAGO AGENC" Then 
         balance = amount - residue
     ElseIf Tipo = "DEBITO" Then 
         balance = amount + residue
     End If

     balance = Application.Round(balance, 2) 

End Function

2 Answers 2

2

Several small corrections:

Function balance(condition As String, amount As Integer, residue As Integer)

If condition = "PAGO" Or condition = "PAGO AGENC" Then
    balance = amount - residue
ElseIf condition = "DEBITO" Then
    balance = amount + residue
End If
balance = Application.Round(balance, 2)
End Function
Sign up to request clarification or add additional context in comments.

1 Comment

Just one note: if both values ​​(both amount and residue) are of type Integer, then using Round is unnecessary. Since we are talking about money, most likely the type of these variables is Double
2

Same as Gary's Student, but added a warning line in case the condition is not "PAGO", not "PAGO AGENC" or "DEBITO":

Function balance(condition As String, amount As Double, residue As Double) As Variant
    If condition = "PAGO" Or condition = "PAGO AGENC" Then
        balance = Application.Round(amount - residue, 2)
    ElseIf condition = "DEBITO" Then
        balance = Application.Round(amount + residue, 2)
    Else
        balance = "Wrong condition!"
    End If
End Function

1 Comment

Very nice use of Variant (+1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.