vba error next without for

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hello every one,
I had been working in this little problem, and now I got error messages ►next without for◄ I count my for and next and everything are even, so I don't get it, I need some hand here. this code is about input some numbers on column A generate non repiten combination on C:H IF column H - column C in in the range indicated on A25 minimun and A26 maximun.
HTML:
Option Explicit
Sub combinations()
Dim rRng As Range, p As Integer
Dim vElements, lRow As Variant, vresult As Variant
Set rRng = Range("A1", Range("A1").End(xlDown))
p = 6
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
ReDim vresult(1 To p)
Call CombinationsNP(vElements, p, vresult, 1, 1)
End Sub
Sub CombinationsNP(vElements As Variant, p As Integer, vresult As Variant, iElement As Integer, ilndex As Integer)
Dim i As Integer
For i = iElement To UBound(vElements)
    vresult(ilndex) = vElements(i)
    Dim x As Long
    Dim y As Long
For x = 1 To Cells(Rows.Count, "A").End(xlUp).Row
 For y = 1 To 6
 Next
 Next
Cells(x, "I").Value = Cells(x, "H").Value - Cells(x, "C").Value
If Cells(x, "I").Value > Range("A25").Value And Cells(x, "I").Value < Range("A26") Then
Call CombinationsNP(vElements, p, vresult, i + 1, ilndex + 1)
If ilndex = p Then
    If vresult(1) >= 1 And vresult(2) >= 3 And vresult(3) >= 5 And vresult(4) >= 9 Then
       Cells(Application.Rows.Count, 3).End(xlUp).Offset(1, 0).Resize(, p) = vresult
End If
Else
Call CombinationsNP(vElements, p, vresult, i + 1, ilndex + 1)
End If
Next i
End Sub
I still don't see the error.
please some advise.
let me explain with a little example. if on A25 value = 30 [min] and A26 45[max] then the different between the column H - column C if they are in this range THEN thats the perfect output.
like this combination
HTML:
7	8	23	35	49	51  [51-7=44]
10	18	28	41	44	51  [51-10=41 etc]
7	20	25	26	31	42
4	20	36	41	44	46
thats the only combinations I want to generate.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
For i = iElement To UBound(vElements)
vresult(ilndex) = vElements(i)
Dim x As Long
Dim y As Long
For x = 1 To Cells(Rows.Count, "A").End(xlUp).Row
For y = 1 To 6
Next x
Next i
 
Upvote 0
Your code would be easier to debug if you structured it better:
Code:
Sub CombinationsNP(vElements As Variant, p As Integer, vresult As Variant, iElement As Integer, ilndex As Integer)
Dim i As Long, x As Long, y As Long
For i = iElement To UBound(vElements)
  vresult(ilndex) = vElements(i)
  For x = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    For y = 1 To 6
    Next
  Next
  Cells(x, "I").Value = Cells(x, "H").Value - Cells(x, "C").Value
  If Cells(x, "I").Value > Range("A25").Value And Cells(x, "I").Value < Range("A26") Then
    Call CombinationsNP(vElements, p, vresult, i + 1, ilndex + 1)
    If ilndex = p Then
      If vresult(1) >= 1 And vresult(2) >= 3 And vresult(3) >= 5 And vresult(4) >= 9 Then
        Cells(Application.Rows.Count, 3).End(xlUp).Offset(1, 0).Resize(, p) = vresult
      End If
    Else
      Call CombinationsNP(vElements, p, vresult, i + 1, ilndex + 1)
    End If
  [COLOR="#FF0000"]End If[/COLOR]
Next i
End Sub
Not only that, your For x = 1 To Cells(Rows.Count, "A").End(xlUp).Row - Next loop doesn't actually process the Cells(x, "I").Value = Cells(x, "H").Value - Cells(x, "C").Value expression. Perhaps you meant to have something like:
Code:
Sub CombinationsNP(vElements As Variant, p As Integer, vresult As Variant, iElement As Integer, ilndex As Integer)
Dim i As Long, x As Long, y As Long
For i = iElement To UBound(vElements)
  vresult(ilndex) = vElements(i)
  For x = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    For y = 1 To 6
    Next
    Cells(x, "I").Value = Cells(x, "H").Value - Cells(x, "C").Value
    If Cells(x, "I").Value > Range("A25").Value And Cells(x, "I").Value < Range("A26") Then
      Call CombinationsNP(vElements, p, vresult, i + 1, ilndex + 1)
      If ilndex = p Then
        If vresult(1) >= 1 And vresult(2) >= 3 And vresult(3) >= 5 And vresult(4) >= 9 Then
          Cells(Application.Rows.Count, 3).End(xlUp).Offset(1, 0).Resize(, p) = vresult
        End If
      Else
        Call CombinationsNP(vElements, p, vresult, i + 1, ilndex + 1)
      End If
    End If
  Next
Next i
End Sub

I also can't see what the point of your For y = 1 To 6 - Next loop is. It doesn't do anything meaningful.
 
Last edited:
Upvote 0
Thank you all of you, I make the changes you sugest and still I don't get the expecting results. now the code looks like this
HTML:
Option Explicit
Sub combinations()
Dim rRng As Range, p As Integer
Dim vElements, lRow As Variant, vresult As Variant
Set rRng = Range("A1", Range("A1").End(xlDown))
p = 6
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
ReDim vresult(1 To p)
Call CombinationsNP(vElements, p, vresult, 1, 1)
End Sub

Sub CombinationsNP(vElements As Variant, p As Integer, vresult As Variant, iElement As Integer, ilndex As Integer)
Dim i As Long, x As Long, y As Long
For i = iElement To UBound(vElements)
  vresult(ilndex) = vElements(i)
  For x = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    For y = 1 To 6
    Next
    Cells(x, "I").Value = Cells(x, "H").Value - Cells(x, "C").Value
    If Cells(x, "I").Value > Range("A25").Value And Cells(x, "I").Value < Range("A26") Then
      Call CombinationsNP(vElements, p, vresult, i + 1, ilndex + 1)
      If ilndex = p Then
        If vresult(1) >= 1 And vresult(2) >= 3 And vresult(3) >= 5 And vresult(4) >= 9 Then
          Cells(Application.Rows.Count, 3).End(xlUp).Offset(1, 0).Resize(, p) = vresult
        End If
      Else
        Call CombinationsNP(vElements, p, vresult, i + 1, ilndex + 1)
      End If
    End If
  Next
Next i
End Sub
Now do not show any error, but do not give me any result, everything is in blank.
 
Upvote 0
Not sure, but you are calling a sub within itself... how will it ever end? You are calling CombinationsNP inside itself.
 
Upvote 0
thanks Ragnar1211 , really end, but I don't get nothing, So what line you sugest to delete
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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