Sub Correction()
Dim i As Integer
For Sex = 1 To 2
For Age = 0 To 100
Range("Test1") = Sex
Range("Test2") = Age
For i = 4 To 145
Sheets("Input").Cells(i, 10).Copy
Range("Amount").PasteSpecial Paste:=xlValues
If Range("Negative") = 0 Then
Sheets("Input").Cells(i, 13).Copy
Sheets("Input").Cells(Age + 4, 14).PasteSpecial Paste:=xlValues
End If
Next
Next
Next
End Sub
当我尝试运行宏时,对于Sex = 1
,Age = 0
,让我们说在宏发现i = 10
生成"Negative" = 0
之后,宏仍然继续下一个我。但我希望宏在找到特定年龄的"Negative" = 0
后继续下一个Age。
请帮我解决这个问题
答案 0 :(得分:1)
编辑
Option Explicit
Sub Correction()
Dim i As Integer, Sex As Integer, Age As Integer
If Cells(Age + 4, 12) > 0 Then
For Sex = 1 To 2
For Age = 0 To 100
Range("Test1") = Sex
Range("Test2") = Age
For i = 4 To 145
Sheets("Input").Cells(i, 10).Copy
Range("Amount").PasteSpecial Paste:=xlValues
If Range("Negative") = 0 Then
Sheets("Input").Cells(i, 13).Copy
Sheets("Input").Cells(Age + 4, 14).PasteSpecial Paste:=xlValues
Exit For '<== this will have you exit the current for loop
End If
Next i
Next Age
Nextsex
End If
End Sub
我建议完全限定您使用的范围。因为,现在是:
Cells(Age + 4, 12)
,Range("Test1")
,Range("Test2")
,Range("Amount")
,Range("Negative")
都指的是当前的有效工作表,可能不是感兴趣的
Sheets("Input").Cells(i, 10)
,Sheets("Input").Cells(Age + 4, 14)
指的是属于当前活动工作簿的名为“输入”的工作表的单元格