InterestCollected = SumofInt

Else

Payment2 = Pmt(Rate2, TotalMonths - Months1, NewBal)

end if

Solved

Posted on 2011-05-02

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
```

17 Comments

InterestCollected = SumofInt

Else

Payment2 = Pmt(Rate2, TotalMonths - Months1, NewBal)

end if

i.e. you've written

If Months2 = 0 Then

InterestCollected = SumofInt

Else

Do the interest calculation

AND

If Months3 = 0 Then

InterestCollected = SumofInt

Else

Do the interest calculation

AND

If Months4 = 0 Then

etc.

This is easier to see if you indent each section at the start of an "if" condition and take it back out again at the end. Then you can see at which level the blocks of code will execute.

I think angellll's code might save you some time, too. But note how the end ifs come at the end of each block.

- on a single line :

```
IF [...] THEN [...]
IF [...] THEN [...] ELSE [...]
IF [...] THEN [...] ELSE IF [...] THEN [...]
```

or

- on multiple lines :

```
IF [...] THEN
[...]
END IF
IF [...] THEN
[...]
ELSE
[...]
END IF
IF [...] THEN
[...]
ELSE IF [...] THEN
[...]
ELSE
[...]
END IF
```

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.

Same thing if months3 is 0, etc...

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:

If Months2 = 0 Then InterestCollected = SumofInt Else

needs to be rewritten as

If Months2 = 0 Then

Return sumofint

Else

For the "Return" to be recognized you may need to add "As Integer" to the end of you function declaration...

ie. Function InterestCollected(Original

I don't use vb6 much anymore but that is the solution for vb.net and I think that what I just described works in vb6. The real issue is whenever you call InterestCollected, parameters are expected.

I have another solution for you if that fails.

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

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
[...]
```

```
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
```

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

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

Select a random value from list and chg background color. | 5 | 21 | |

How to customise Office 2016 font settings with a GPO | 3 | 25 | |

Excel -- increase row height through cell entry (via VBA) | 3 | 23 | |

VB6 - Compare and highlight cell not the same | 3 | 0 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**12** Experts available now in Live!