Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环vba excel_Vba_Excel - Fatal编程技术网

循环vba excel

循环vba excel,vba,excel,Vba,Excel,当我尝试运行宏时,对于Sex=1,Age=0,假设在宏发现I=10使“Negative”=0后,宏仍继续执行下一个I。但我希望宏在找到特定年龄的“Negative”=0后继续下一个年龄 请在OP澄清后帮助我修复已编辑的宏 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 Sheet

当我尝试运行宏时,对于
Sex=1
Age=0
,假设在宏发现
I=10
使
“Negative”=0
后,宏仍继续执行下一个I。但我希望宏在找到特定年龄的
“Negative”=0
后继续下一个年龄


请在OP澄清后帮助我修复已编辑的宏

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
选项显式
次修正()
Dim i为整数,性别为整数,年龄为整数
如果细胞(年龄+4,12)>0,则
性别=1到2
年龄=0至100岁
范围(“测试1”)=性别
范围(“测试2”)=年龄
对于i=4到145
表格(“输入”)。单元格(i,10)。复制
范围(“金额”)。粘贴特殊粘贴:=xlValues
如果范围(“负”)=0,则
表格(“输入”)。单元格(i,13)。复制
表格(“输入”)。单元格(年龄+4,14)。粘贴特殊粘贴:=xlValues

退出“请提供适当的背景并明确说明您的问题。没有人会对理解您的代码以向您提供解决方案感兴趣。通过这个链接,你可以问一个好问题,你想达到什么目的?我现在无法理解你的代码。解释这个宏的目标有点复杂,我需要分享这个电子表格。基本上,在编码中,有3个For语句,但是如果出现范围(“负”)=0,我想退出这个“For I=4到145”,然后继续下一个“For Age=0到100”。简单地说,我有3个for语句,如果我的“if”语句得到满足,我想退出1个for语句真的谢谢,这可以解决我的问题,在我将exit放在后面之前,然后如果我想让另一个条件运行
for I=4到465
如果单元格(Age,12)=0,那么我需要添加
if单元格(Age,12).value=0然后
End如果
你是指
如果范围(“负”)=0和单元格(年龄,15)=0然后…
。否则,请添加更多信息。最后:你可能想对任何你觉得有用的答案投赞成票,并将满足你原来问题的答案标记为已接受。谢谢抱歉,忽略第二条注释,我的意思是如果
单元格(年龄+4,12)>0
,那么我想运行整个宏。那么我需要把这个if语句放在哪里呢?请参见编辑后的答案。如果原始问题已完成,您可能希望将其标记为已接受
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