2015-10-05 138 views
0

我正在運行一些If和For循環。起始可變一個= 3I = 3,當代碼退出一個IF首次,一個等於5 當i = 4個的和宏運行IF再次一個取初始值a = 3。我想讓它繼續增加1,最後a值。如何將變量值增加1?

例如,如果在首先對於I = 3,如果在一個= 5停止,在下次因爲,當i = 4的,我想有一個起始值一個= 6(a = 5 +1)

再比如說,當我= 5,如果前面的IF上A = 15停了下來,我想有一個初始值= 16誰能與它幫助嗎?

... 

    For i = 3 To 10 
    ... 

    If Sheets("Migrazioni").Cells(i, 5) < 125 Then 
    a = 3 
     For b = Sheets("Migrazioni").Range("N" & i).Value To y 
      With Sheets("Report KIT") 
      If (.Cells(b, 4).Value2 = "ATTIVO") And (.Cells(b, 6).Value2 >= 130) Then 
     .Cells(b, 7).Copy 
       Sheets("KIT").Cells(a, 1).PasteSpecial xlPasteValues 
     Sheets("Migrazioni").Cells(i, 4).Copy 
       Sheets("KIT").Cells(a, 2).PasteSpecial xlPasteValues 
      a = a + 1 
      End If 
      'b = b + 1 
      End With 
     Next b 

    End If 

    ... 

    Next i 

    End Sub 
+1

爲什麼代碼不只是設置= 3的第一外對於.. Next循環? –

回答

0

移動= 3以上對於i = 3〜10像下面

... 

a = 3 
For i = 3 To 10 
... 

If Sheets("Migrazioni").Cells(i, 5) < 125 Then 
    For b = Sheets("Migrazioni").Range("N" & i).Value To y 
     With Sheets("Report KIT") 
     If (.Cells(b, 4).Value2 = "ATTIVO") And (.Cells(b, 6).Value2 >= 130) Then 
    .Cells(b, 7).Copy 
      Sheets("KIT").Cells(a, 1).PasteSpecial xlPasteValues 
    Sheets("Migrazioni").Cells(i, 4).Copy 
      Sheets("KIT").Cells(a, 2).PasteSpecial xlPasteValues 
     a = a + 1 
     End If 
     'b = b + 1 
     End With 
    Next b 

End If 

... 

Next i 

End Sub