Case statements in For loop causing "Next i" to not be recognized

Fernando14

New Member
Joined
Jun 29, 2021
Messages
12
Office Version
  1. 2019
Platform
  1. Windows
I am having an issue using case statements in my for loop. Here is the code:
VBA Code:
For i = 1 To (SECT + 1)
    L(i) = 5                                
    If STR > 0 Then L(STSECT + 2) = TR
    If STR > 0 Then L(STSECT + 1) = STR
    DIA(i) = DIA(i - 1) - L(i) / ROT
    If STR > 0 Then DIA(STSECT + 1) = D
    If i <= STSECT Then DIA(i) = D          
    AVGDIA(i) = (DIA(i - 1) + DIA(i)) / 2      
    A(i) = AVGDIA(i) * L(i) / 12
    VD(i) = V * (AVGDIA(i) / 12)
    CD(i) = 129 / (((VD(i)) ^ (1.3)))
    If VD(i) <= 39 Then CD(i) = 1.1
    If VD(i) >= 78 Then CD(i) = 0.45        
    LC(i) = LTOT + L(i) / 2                 
    LTOT = LTOT + L(i)
    
    Select Case LC(i) + ELEV
    Case LC(i) + ELEV <= 300
        If EXP = "B" Then CH(i) = 1.35 Else CH(i) = 1.59
    Case LC(i) + ELEV <= 250
        If EXP = "B" Then CH(i) = 1.28 Else CH(i) = 1.53
    Case LC(i) + ELEV <= 200
        If EXP = "B" Then CH(i) = 1.2 Else CH(i) = 1.46
    Case LC(i) + ELEV <= 180
        If EXP = "B" Then CH(i) = 1.17 Else CH(i) = 1.43
    Case LC(i) + ELEV <= 160
        If EXP = "B" Then CH(i) = 1.14 Else CH(i) = 1.39
    Case LC(i) + ELEV <= 140
        If EXP = "B" Then CH(i) = 1.09 Else CH(i) = 1.36
    Case LC(i) + ELEV <= 120
        If EXP = "B" Then CH(i) = 1.04 Else CH(i) = 1.31
    Case LC(i) + ELEV <= 100
        If EXP = "B" Then CH(i) = 0.99 Else CH(i) = 1.26
    Case LC(i) + ELEV <= 90
        If EXP = "B" Then CH(i) = 0.96 Else CH(i) = 1.24
    Case LC(i) + ELEV <= 80
        If EXP = "B" Then CH(i) = 0.93 Else CH(i) = 1.21
    Case LC(i) + ELEV <= 70
        If EXP = "B" Then CH(i) = 0.89 Else CH(i) = 1.17
    Case LC(i) + ELEV <= 60
        If EXP = "B" Then CH(i) = 0.85 Else CH(i) = 1.13
    Case LC(i) + ELEV <= 50
        If EXP = "B" Then CH(i) = 0.81 Else CH(i) = 1.09
    Case LC(i) + ELEV <= 40
        If EXP = "B" Then CH(i) = 0.76 Else CH(i) = 0.04
    Case LC(i) + ELEV <= 30
        If EXP = "B" Then CH(i) = 0.7 Else CH(i) = 0.98
    Case LC(i) + ELEV <= 25
        If EXP = "B" Then CH(i) = 0.7 Else CH(i) = 0.94
    Case LC(i) + ELEV <= 20
        If EXP = "B" Then CH(i) = 0.7 Else CH(i) = 0.9
    Case LC(i) + ELEV <= 15
        If EXP = "B" Then CH(i) = 0.7 Else CH(i) = 0.85
        
    P(i) = 0.00256 * ((V) ^ 2) * CD(i) * CH(i) * 1.14
    CF = CH(i)                              'End of loop 3
    WP(i) = P(i) * A(i)                     'Start of loop 4
    TOTWP = TOTWP + WP(i)
    MOM(i) = WP(i) * LC(i)
    TOTMOM = TOTMOM + MOM(i)                'End of loop 4
Next i

I get an error that there is a next i without a for statement. I'm assuming the If statements are what causes this error but I am not sure. I am using the Case statements in an attmept to have less code and be more concise, but I am also unfamiliar with it. Any help is appreciated.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
You are missing an End Select and the syntax:
Select Case LC(i) + ELEV
Case LC(i) + ELEV <= 300
is wrong. Should be:
Select Case LC(i) + ELEV
Case Is <= 300
 
Upvote 0
Solution
... in addition the previous comments: the If ... Then construct is permitted on one line without a closing End If.
VBA Code:
    If A = 1 Then B = 0
    
    ' equals \/
    
    If A = 1 Then
        B = 0
    End If


At the other hand, the If ... Then ... Else construct needs to be closed, one way or the other (note the colons).
VBA Code:
    If A = 1 Then: B = 0: Else C = 0:
    
    ' equals \/
    
    If A = 1 Then
        B = 0
    Else
        C = 0
    End If
 
Upvote 0
The syntax

VBA Code:
Select Case True
    Case LC(i) + ELEV <= 300
...
    Case LC(i) + ELEV <= 250
...
    Case Else
...
End Select

also works.


Edit: Actually it doesn't. Nor does JoeMo's. The concept is correct, but you need to do the values in reverse order. If LC(i) +ELEV is 50 for example, the first condition will be true. You need to put the values in reverse order. Or instead of Select Case, use a lookup table instead.
 
Last edited:
Upvote 0
You are missing an End Select and the syntax:
Select Case LC(i) + ELEV
Case LC(i) + ELEV <= 300
is wrong. Should be:
Select Case LC(i) + ELEV
Case Is <= 300
This fixed it, Thank you
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top