2014-05-25 45 views
0

我想创建一个计数,通过列运行检查值是否在指定的标准之间比它符合它(工程)。VBA更改循环的参数

但我想窝它在另一个for循环,所以一旦它完成第一类

x=4 
y=13 
So For a= x To y 
Runs Code 

then i want it to run 

For a = 14 To 27 
code code code 

x= x+14 
y= y+14 

For a = 28 to 41 
etc.... 

这里是我的代码

Sub count() 

    Dim c, d As Long 
    Dim x As Integer 
    Dim y As Integer 

    c = 0 
    d = 0 
    x = 4 
    y = 13 

    lr = ThisWorkbook.Sheets("Probability").Cells(Rows.count, 2).End(xlUp).Row 

    For a = x To y 
     For b = 2 To lr 
      If Cells(b, 2) >= Cells(a, 10) Then 
       c = c + 1 
      End If 
      If Cells(b, 2) >= Cells(a, 11) Then 
       d = d + 1 
      End If 
      Range("L" & a).Value = c 
      Range("M" & a).Value = d 
     Next b 
     c = 0 
     d = 0 

     x = x + 14 
     y = y + 14 
     Range("O1").Value = x 
     Range("P1").Value = y 
    Next a 
    End Sub 
+0

For Loop不能这样工作。即使你改变了它内部的x,a仍然会有第一个x的值为4.现在你想如何增加a,但不清楚。首先,你将它分配给4,然后是14,然后是28?如果你坚持+14区间,是不是4,18,32?或者我错过了什么。 – L42

回答

0

我重读你的问题,你可能会错过另一个循环如下。

Do 
    For a = x To y 
     For b = 2 To lr 
      If Cells(b, 2) >= Cells(a, 10) Then c = c + 1 
      If Cells(b, 2) >= Cells(a, 11) Then d = d + 1 
      Range("L" & a).Value = c 
      Range("M" & a).Value = d 
     Next b 
     c = 0 
     d = 0 
    Next a 
    x = x + 14 
    y = y + 14 
    Range("O1").Value = x 
    Range("P1").Value = y 
Loop Until "Set condition here" 

我不过我还是误解了你的问题,注释掉澄清。

+0

谢谢!我得到它的工作,开始插入一个while循环! – user3457548

+0

太棒了!如果解决方案适用于您,请参阅[接受答案](http://stackoverflow.com/help/someone-answers)。这将帮助您赢得声誉并吸引更多用户在未来帮助您。 – L42