[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

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

Open in new window

0
appc
Asked:
appc
  • 5
  • 4
  • 3
  • +3
6 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
  If Months2 = 0 Then
    InterestCollected = SumofInt
Else
 
   Payment2 = Pmt(Rate2, TotalMonths - Months1, NewBal)
end if
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
in short, you have to move the end if a couple of lines up ,...
0
 
computerdoctorserviceCommented:
You have nested your ifs and end ifs, so the way you have written the code, each section will run if and only if the previous statement doesn't.

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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Arno KosterCommented:
The if, then, else statements should be either

 - on a single line :
IF [...] THEN [...] 
IF [...] THEN [...] ELSE [...]
IF [...] THEN [...] ELSE IF [...] THEN [...]

Open in new window



or

- on multiple lines :
IF [...] THEN
   [...]
END IF
IF [...] THEN
   [...]
ELSE
   [...]
END IF
IF [...] THEN
   [...]
ELSE IF [...] THEN
   [...]
ELSE
   [...]
END IF

Open in new window

0
 
appcAuthor Commented:
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.
0
 
appcAuthor Commented:
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.
0
 
appcAuthor Commented:
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...
0
 
computerdoctorserviceCommented:
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:
0
 
appcAuthor Commented:
Can you insert it in my code maybe?

I am not sure I get it.

Thanks!
0
 
David L. HansenProgrammer AnalystCommented:
I saw no problem with the nesting of ifs.  The issue, as I see it, is that the following code:

 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 As Double, TotalMonths As Double, Payment1 As Double, Months1 As Double, Rate1 As Double, Months2 As Double, Rate2 As Double) As Integer

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.
   
0
 
David L. HansenProgrammer AnalystCommented:
whoops.  "As Double" instead of "As Integer" is what you likely want.
0
 
computerdoctorserviceCommented:
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
0
 
computerdoctorserviceCommented:
I agree - there's nothing really wrong with your nesting.  
0
 
Arno KosterCommented:
Appc,

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

Open in new window

0
 
Paul JacksonCommented:
Just need to structure your code correctly the code below should work:
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

Open in new window

0
 
David L. HansenProgrammer AnalystCommented:
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

0
 
appcAuthor Commented:
Thanks guys.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now