appc
asked on
How do I use else in VB?
Hello,
I have a problem with the following code. I get a compile error that says "end if without block if".
But I have 4 ifs in my code so I don't know where to place those end ifs without messing up my code.
I have a problem with the following code. I get a compile error that says "end if without block if".
But I have 4 ifs in my code so I don't know where to place those end ifs without messing up my code.
Function InterestCollected(Original As Double, TotalMonths As Double, Payment1 As Double, Months1 As Double, Rate1 As Double, Months2 As Double, Rate2 As Double)
Dim Interest As Double
Dim NewBal As Double
Dim Balpaydown As Double
Dim Bal As Double
Dim PreviousInt As Double
Dim i As Integer
Bal = Original
NewBal = Original
PreviousInt = 0
For i = 1 To Months1
Interest = NewBal * (Rate1 / 12)
Balpaydown = Payment1 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months2 = 0 Then InterestCollected = SumofInt Else
Payment2 = Pmt(Rate2, TotalMonths - Months1, NewBal)
For i = 1 To Months2
Interest = NewBal * (Rate2 / 12)
Balpaydown = Payment2 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months3 = 0 Then InterestCollected = SumofInt Else
Payment3 = Pmt(Rate3, TotalMonths - Months1, NewBal)
For i = 1 To Months3
Interest = NewBal * (Rate3 / 12)
Balpaydown = Payment3 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months4 = 0 Then InterestCollected = SumofInt Else
Payment4 = Pmt(Rate4, TotalMonths - Months1, NewBal)
For i = 1 To Months4
Interest = NewBal * (Rate4 / 12)
Balpaydown = Payment4 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months4 = 0 Then InterestCollected = SumofInt Else
Payment5 = Pmt(Rate5, TotalMonths - Months1, NewBal)
For i = 1 To Months5
Interest = NewBal * (Rate5 / 12)
Balpaydown = Payment5 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
InterestCollected = PreviousInt
End If
End If
End If
End If
End Function
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
in short, you have to move the end if a couple of lines up ,...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well ok but I don't want to make sure that every time the if condition is not met the rest of the code stops and my function equals the InterestCollected value.
ASKER
Sorry I meant:
Well ok but I want to make sure that every time the if condition is not met the rest of the code stops and my function equals the InterestCollected value.
Well ok but I want to make sure that every time the if condition is not met the rest of the code stops and my function equals the InterestCollected value.
ASKER
For example example if months2 is 0 then and I don't want any of the following loops to be run.
Same thing if months3 is 0, etc...
Same thing if months3 is 0, etc...
Then the way you are doing it is essentially correct.
You can use the "goto" command and insert a label to make sure the code exits the "if" blocks, eg.
In your code, at the bottom of your calculation, use "GOTO StopPoint"
and at the bottom of your code, create a label
StopPoint:
You can use the "goto" command and insert a label to make sure the code exits the "if" blocks, eg.
In your code, at the bottom of your calculation, use "GOTO StopPoint"
and at the bottom of your code, create a label
StopPoint:
ASKER
Can you insert it in my code maybe?
I am not sure I get it.
Thanks!
I am not sure I get it.
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Function InterestCollected(Original As Double, TotalMonths As Double, Payment1 As Double, Months1 As Double, Rate1 As Double, Months2 As Double, Rate2 As Double)
Dim Interest As Double
Dim NewBal As Double
Dim Balpaydown As Double
Dim Bal As Double
Dim PreviousInt As Double
Dim i As Integer
Bal = Original
NewBal = Original
PreviousInt = 0
For i = 1 To Months1
Interest = NewBal * (Rate1 / 12)
Balpaydown = Payment1 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months2 = 0 Then
InterestCollected = SumofInt
GoTo StopPoint
Else
Payment2 = Pmt(Rate2, TotalMonths - Months1, NewBal)
End If
For i = 1 To Months2
Interest = NewBal * (Rate2 / 12)
Balpaydown = Payment2 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months3 = 0 Then
InterestCollected = SumofInt
Else
Payment3 = Pmt(Rate3, TotalMonths - Months1, NewBal)
GoTo StopPoint
End If
For i = 1 To Months3
Interest = NewBal * (Rate3 / 12)
Balpaydown = Payment3 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months4 = 0 Then
InterestCollected = SumofInt
Else
Payment4 = Pmt(Rate4, TotalMonths - Months1, NewBal)
GoTo StopPoint
End If
For i = 1 To Months4
Interest = NewBal * (Rate4 / 12)
Balpaydown = Payment4 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months4 = 0 Then
InterestCollected = SumofInt
Else
Payment5 = Pmt(Rate5, TotalMonths - Months1, NewBal)
GoTo StopPoint
End If
For i = 1 To Months5
Interest = NewBal * (Rate5 / 12)
Balpaydown = Payment5 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
InterestCollected = PreviousInt
StopPoint:
REM FINISHED
End Function
Dim Interest As Double
Dim NewBal As Double
Dim Balpaydown As Double
Dim Bal As Double
Dim PreviousInt As Double
Dim i As Integer
Bal = Original
NewBal = Original
PreviousInt = 0
For i = 1 To Months1
Interest = NewBal * (Rate1 / 12)
Balpaydown = Payment1 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months2 = 0 Then
InterestCollected = SumofInt
GoTo StopPoint
Else
Payment2 = Pmt(Rate2, TotalMonths - Months1, NewBal)
End If
For i = 1 To Months2
Interest = NewBal * (Rate2 / 12)
Balpaydown = Payment2 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months3 = 0 Then
InterestCollected = SumofInt
Else
Payment3 = Pmt(Rate3, TotalMonths - Months1, NewBal)
GoTo StopPoint
End If
For i = 1 To Months3
Interest = NewBal * (Rate3 / 12)
Balpaydown = Payment3 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months4 = 0 Then
InterestCollected = SumofInt
Else
Payment4 = Pmt(Rate4, TotalMonths - Months1, NewBal)
GoTo StopPoint
End If
For i = 1 To Months4
Interest = NewBal * (Rate4 / 12)
Balpaydown = Payment4 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months4 = 0 Then
InterestCollected = SumofInt
Else
Payment5 = Pmt(Rate5, TotalMonths - Months1, NewBal)
GoTo StopPoint
End If
For i = 1 To Months5
Interest = NewBal * (Rate5 / 12)
Balpaydown = Payment5 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
InterestCollected = PreviousInt
StopPoint:
REM FINISHED
End Function
I agree - there's nothing really wrong with your nesting.
Appc,
you could also use the "EXIT FUNCTION" statement, this does not require use of labels but instantly stops the processing of the code :
you could also use the "EXIT FUNCTION" statement, this does not require use of labels but instantly stops the processing of the code :
[...]
For i = 1 To Months1
Interest = NewBal * (Rate1 / 12)
Balpaydown = Payment1 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
If Months2 = 0 Then
InterestCollected = SumofInt
Exit Sub
Else
Payment2 = Pmt(Rate2, TotalMonths - Months1, NewBal)
For i = 1 To Months2
Interest = NewBal * (Rate2 / 12)
Balpaydown = Payment2 - Interest
Bal = NewBal
NewBal = Bal - Balpaydown
SumofInt = Interest + PreviousInt
PreviousInt = SumofInt
Next i
End If
If Months3 = 0 Then
InterestCollected = SumofInt
Exit Sub
Else
[...]
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This should do it:
Take your original code and add the "As Double" to the end of you first like where the function is declared:
....Rate1 As Double, Months2 As Double, Rate2 As Double) As Double
then, change your:
If Months2 = 0 Then InterestCollected = SumofInt Else
to:
If Months2 = 0 Then
Set InterestCollected = sumofint
Exit Function
Else
Take your original code and add the "As Double" to the end of you first like where the function is declared:
....Rate1 As Double, Months2 As Double, Rate2 As Double) As Double
then, change your:
If Months2 = 0 Then InterestCollected = SumofInt Else
to:
If Months2 = 0 Then
Set InterestCollected = sumofint
Exit Function
Else
ASKER
Thanks guys.