Disconnected For Next Loop

GeorgeA

New Member
Joined
Mar 27, 2016
Messages
7
I'm working to finish up my final project for a financial modeling class. Below is the code for a section. I haven't made many changes lately, but all of a sudden, every time I go to run it, I'm getting an error saying that the next to last "Next" does not have a "For". I've gone over it so many times, I can't begin to understand what I'm missing. I'm thinking I just need a different set of eyes to look at it, as I've been staring at it a lot this last week. Any help or suggestions are much appreciated.



Code:
For j = 1 To 4
    cash(j) = 50000
    row30 = RSIrow(30, RSIcol(j) + 1)
    row70 = RSIrow(70, RSIcol(j) + 1)


'if RSI > 70 comes before RSI < 30, start will a long position


    If row70 < row30 Then
        purchase(j) = Range(drange).Offset(1, prcCol(j)).Value * 0.95
        shares(j) = cash(j) / purchase(j)
        cash(j) = 0
    Else
        shares(j) = 0
        purchase(j) = 0
    End If
Next j


For i = 1 To numRows
    balance = 0
    'This is where our 1.005 for each stock we hold should go.
    For x = 1 To 4
        If cash(x) > 0 Then cash(x) = cash(x) * (1 + Range(rfr).Offset(i, 0).Value / 100)
        If margin(x) < 0 Then margin(x) = margin(x) * 1.005    'interest expense
    Next x
      
    For j = 1 To 4
    ' sell if RSI > 70
        If Range(drange).Offset(i, RSIcol(j)).Value > 70 Then
            If shares(j) > 0 Then
                cash(j) = shares(j) * Range(drange).Offset(i, prcCol(j)).Value
                shares(j) = 0
                purchase(j) = 0
                    
            ElseIf cash(j) > 0 And Not prudent Then  ' short stock
                shares(j) = (-2 * cash(j) * 0.96 / Range(drange).Offset(i, prcCol(j)).Value)
                margin(j) = 3 * cash(j) - 2 * 0.04 * cash(j)
                cash(j) = 0
                purchase(j) = Range(drange).Offset(i, prcCol(j)).Value
            End If
        End If
        
    ' buy if RSI < 30
        If Range(drange).Offset(i, RSIcol(j)).Value < 30 Then
            If cash(j) > 0 Then
                If prudent Then
                    shares(j) = cash(j) / Range(drange).Offset(i, prcCol(j)).Value
                    cash(j) = 0
                    purchase(j) = Range(drange).Offset(i, prcCol(j)).Value
                
            ElseIf shares(j) < 0 Then   ' close short position if it exists
                margin(j) = margin(j) - shares(j) * Range(drange).Offset(i, prcCol(j)).Value
                cash(j) = margin(j)
                margin(j) = 0
                shares(j) = 0
                purchase(j) = 0
            End If
        End If
            
        'Code for StopLoss and StopBuy
    If prcCol(j) < (1 + Range(stopBuy).Value) * purchase(j) And shares(j) > 0 Then 'stopbuy
        cash(j) = shares(j) * Range(drange).Offset(i, prcCol(j)).Value
            shares(j) = 0
            purchase(j) = 0
            
    If prcCol(j) > (1 - Range(stopBuy).Value) * purchase(j) And shares(j) < 0 Then 'stoploss
        cash(j) = shares(j) * Range(drange).Offset(i, prcCol(j)).Value
            shares(j) = 0
            purchase(j) = 0
    End If
  End If
  
  balance = balance + cash(j) + shares(j) * Range(drange).Offset(i, prcCol(j)).Value + margin(j)
  
    Next j
    Range(outrange).Offset(i, 0).Value = balance
    
Next i
 
Thanks. It's true. I see VBA listed more and more on Job Postings these days. I think people are finally starting to realize it for what it is..... a very valuable skill.
 
Last edited:
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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