VBA coding "For without next" error

Meowmechon

New Member
Joined
Sep 12, 2013
Messages
4
Hi people !
Could anyone kindly point me to where this nuisance error comes from ? I check practically every corner of the coding but just don't see how come there is any "for" or "next" absent. I also tried to solve the problem myself by looking up some online forums on similar questions, but it seems most of the cases are invariably missing an "end if", which doesn't really fit my kind of circumstance. Also, is there any other potential errors seeming obvious in the coding lines apart from the known "for...next" ? Cause I read that VBA has a tendency to propagate glitches and I would love to target down as early as possible too ! ;) I know the coding may appear to be a bit lengthy and might get your eyes somewhat sore, but I'm really a freshman with limited knowledge on VBA so please forgive me ! :p
Many thanks !

Code:
Function FR_xy(P, n, x, xn, y, yn, S_M, R_F)

Dim R(), Best(), PH, PL, P0, Cnb, Cns, Cb, Cs, Dn, Db, Ds, Ct, Mean(), Sharpe(), Max(), Hit, RH, RL, B_Win, B_Lose, S_Win, S_Lose
ReDim R(n - 1), Mean(n), Sharpe(n), Max(13), Best(n) 
                       
Hit = 0                                

For w = 1 To xn                      

    For q = 1 To yn

If y(q) >= x(w) Then GoTo 1            

Ct = Ct + 1                            
                                       
i = 0     
PH = P(1) 
PL = P(1) 
P0 = P(1)
RH = 0   
RL = 0   
Dn = 1   
Db = 0                                
Ds = 0                                
Cb = 0
Cs = 0
Cnb = 0 
Cns = 0 
B_Win = 0
B_Lose = 0 
S_Win = 0
S_Lose = 0


For t = 2 To n                         


If P(t) = 0 Then                      

    If i = 0 Then                   

    Dn = Dn + 1

    GoTo 0

    If i = 1 Then

    Db = Db + 1
    
    GoTo 0

    If i = -1 Then

    Ds = Ds + 1
    
    GoTo 0                 

End If



If i = 0 Then             

R(t) = 0      

Dn = Dn + 1   

                   
    If P(t) > P(t - 1) Then           
    
        If PH < P(t) Then              
    
        PH = P(t)  
    
        End If
    
    ElseIf P(t) < P(t - 1) Then
     
        If PL > P(t) Then
        
        PL = P(t)
        
        End If
    
    End If


    If (P(t) - PL) / PL >= x(w) Then  
    
    i = 1
    
    Cnb = Cnb + 1          
    
    PH = P(t)       
    
    P0 = P(t)               
                            
    PL = P(t)               
    
    ElseIf (PH - P(t)) / PH >= y(q) Then 
    
    i = -1
    
    Cns = Cns + 1          
    
    PH = P(t)                      
    
    P0 = P(t)
    
    PL = P(t)
    
    End If
        
End If



If i = 1 Then        

R(t) = Application.Ln(P(t) / P(t - 1))

Db = Db + 1       

RH = RH + R(t)      

                                       
    If P(t) > P(t - 1) Then    
    
        If PH < P(t) Then
    
        PH = P(t)              
    
        End If
    
    End If
    
        
    If (PH - P(t)) / PH >= y(q) Then  
    
        If P(t) - P0 > 0 Then        
        
        B_Win = B_Win + 1
        
        ElseIf P(t) - P0 <= 0 Then   
        
        B_Lose = B_Lose + 1
        
        End If
        
    i = 0        
    
    Cb = Cb + 1                     
                                    
    PH = P(t)                       
    
    PL = P(t)
    
    End If
        
End If



If i = -1 Then                         

R(t) = Application.Ln(P(t - 1) / P(t)) 

Ds = Ds + 1                            

RL = RL + R(t)                         

                                       
    If P(t) < P(t - 1) Then            
     
        If PL > P(t) Then
        
        PL = P(t)                   
        
        End If
    
    End If

    
    If (P(t) - PL) / PL >= x(w) Then
            
        If P(t) - P0 < 0 Then      
        
        S_Win = S_Win + 1
        
        ElseIf P(t) - P0 >= 0 Then   
        
        S_Lose = S_Lose + 1
        
        End If
        
    i = 0
    
    Cs = Cs + 1          
    
    PH = P(t)                      
    
    PL = P(t)
    
    End If
        
End If


0
    
    
Next                                  

                                                                             
If S_M = "M" Then                              


Mean(Ct) = Application.Sum(R) / (Ds + Db) * 252 

If Mean(Ct) > 0 Then                            

   Hit = Hit + 1
   
End If
                                            
If Ct > 2 Then                                  

    If Mean(Ct) > Mean(Ct - 1) Then             
    
        Max(0) = x(w)                          
    
        Max(1) = y(q)

        Max(2) = Mean(Ct)
        
        Max(3) = Db
        
        Max(4) = Ds
        
        Max(5) = Cb
        
        Max(6) = Cs
        
        Max(7) = B_Win
        
        Max(8) = B_Lose
        
        Max(9) = S_Win
        
        Max(10) = S_Lose
        
        Max(11) = RH
        
        Max(12) = RL
        
        Max(13) = Hit
        
        For u = 2 To n
        
        Best(u) = R(u)
        
        Next
        
    End If
    
End If

ElseIf S_M = "S" Then


'Sharpe Formula : TBA


End If

1  

Next 

Next 


If R_F = "R" Then

FR_xy = Best

ElseIf R_F = "F" Then            

FR_xy = Ct

End If


End Function
 
Last edited by a moderator:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Usually, retyping this code will fix this. This is usually an issue with your white or trailing spaces. Also, make it a habit to nest them properly. If you're a beginner, typing code in this manner is a surefire way to grow up a confused programmer. :)

I suggest using a text editor like Notepad++ to make sure that you've partnered up the beginnings and ends of your loops and ifs properly. Make sure there are no spaces where there shouldn't be (at the end of lines, beginning of indented parts, etc.).

Let us know of your findings.

Thanks,

J.
 
Upvote 0
You appear to be missing 3 End If statements after your 0 line number.

Cause I read that VBA has a tendency to propagate glitches

Where did you read that? I'm not even sure what it's supposed to mean but it sounds like the sort of thing I occasionally see posted by people who don't understand VBA. ;)
 
Upvote 0
Thank you for your generous reply, Mr. Rory !

I added 3 ifs as prescribed, and it works ! But sadly it also tells me "#value!" in the excel table, I tried to monitor the values on some of the variables but all of them only say "can't be determined", any likely scenario that leads to this message ? I think I checked the variables already and they're all being used in the coding. Sorry for troubling you again, Mr. Rory! But I actually got this message too before the "for next" problem, maybe a high time to ask why as well :rolleyes:

On a side note how was is known that there are three ifs missing before the 0 line ? Could you please refer me to the corresponding ifs ? Many appreciation !

I think I read that in VBAX forum !
 
Last edited:
Upvote 0
I indented the code using Smart Indenter and then traced the mismatching statements. I'm afraid there is no way I am going to try and debug that code for you - I suggest you put a breakpoint near the start, recalculate your formulas and then step through the code.
 
Upvote 0
Thank you for your kind and warm reply, JMonty, I'll bear them in mind !:biggrin:

I took the measure suggested by you and Rory to add some more ifs into the coding, but now it's telling me "#value!" in the excel columns when I executed it, I actually got this message before I bumped into the "for next" issue, is there any possible reasons of this error ? I think I fill out the parameters correctly and all the variables erected (e.g P, n, x, xn ...) are used in the coding too. :confused:

On the white spaces, if I attempt to add a note after each line does it count too ? like " For w = 1 To n(space here)' Do not leave spaces"
By beginning of indented parts, do you mean this ? If I use cap does it count ?
For i = 1 to n
(Indented here) For j = i to n

Sorry for asking so many questions, and thanks again for your patience !
 
Upvote 0
Thanks again, Mr. Rory !

Don't worry ! I'm not asking anyone to debug for me ! I'm already much helped by correcting the if statements. I'm just
wondering about the common causes of "#value", would be more convenient if I know where to start looking for the flaws. :)
 
Upvote 0

Forum statistics

Threads
1,216,561
Messages
6,131,417
Members
449,651
Latest member
Jacobs22

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