vba 如何反转 For 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17944074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:17:09  来源:igfitidea点击:

How to reverse a For loop

excelvbaexcel-vba

提问by Artur Rutkowski

I have a problem with my macro. It deletes row that fullfil certain criteria. But when few consecutive rows fullfil those criteria an error occurs. When the row is deleted the other rows are shifted up so if i.e. row(2:2) and row(3:3) fullfil the criteria then the row(2:2) is deleted and row(3:3) is shifted up, so it becomes row(2:2), and For loop goes to another row (third one). As a result row that used to be row(3:3) and now is row(2:2) is omitted and not deleted.
In order to deal with this topic I think that it is enough to reverse to For loop, so it wouldn't go from up to bottom but from bottom to top. Te resulat would be double checking of some rows, but no rows would be omitted.
The proble is that I don't know how to revese the For loop. I have tried to change 'For x = startrow To endrow' to 'For x = endrow To startrow', but it didn't help.
Here is the code:

我的宏有问题。它删除满足特定条件的行。但是当几个连续的行满足这些条件时,就会发生错误。当行被删除时,其他行向上移动,因此如果即行(2:2)和行(3:3)满足条件,则行(2:2)被删除并且行(3:3)向上移动,所以它变成 row(2:2),而 For 循环转到另一行(第三行)。结果,曾经是 row(3:3) 而现在是 row(2:2) 的行被省略并且不会被删除。
为了处理这个话题,我认为反转到For循环就足够了,所以它不会从上到下,而是从下到上。结果是对某些行进行双重检查,但不会遗漏任何行。
问题是我不知道如何修改 For 循环。我曾尝试将“For x = startrow To endrow”更改为“For x = endrow To startrow”,但没有帮助。
这是代码:

Sub Repurchase_upload()

Dim Worksheet As Worksheets
startrow = Worksheets("GUTS").Cells(10, 1)
endrow = Worksheets("GUTS").Cells(11, 1)

 For x = startrow To endrow 'I have tried to change this line into: 'For x = endrow To startrow', but it didn' help
            If Cells(x, "A").Value <> "AA" And Cells(x, "A").Value <> "AB" And Cells(x, "A").Value <> "AC" And Cells(x, "A").Value <> "AD" And Cells(x, "A").Value <> "AE" And     Cells(x, "A").Value <> "AH" And Cells(x, "A").Value <> "AI" And Cells(x, "A").Value <> "AF" And Cells(x, "A").Value <> "AG" Then
            Cells(x, "A").EntireRow.Delete

            End If



    Next
End Sub  

Thank you all a lot in advance,
with best regards,
Artur Rutkowski

预先非常感谢大家,

致以最诚挚的问候,Artur Rutkowski

回答by dendarii

If you're looping and deleting rows, you need to start at the bottom and work up:

如果您要循环和删除行,则需要从底部开始并进行处理:

For x = endrow To startrow step -1

Then deleting rows will not disrupt your loop.

然后删除行不会中断您的循环。

回答by Cuinn Herrick

Loop forward:

向前循环:

For i = 1 To 10
    'Do something
Next i

Loop backwards (use Step -1at the end of the for loop):

向后循环(Step -1在 for 循环末尾使用):

For i = 10 To 1 Step -1
    'Do something
Next i

回答by ExcelExpert

Another alternative is to put x=x-1 after each delete. But working from the bottom up is better.

另一种选择是在每次删除后放置 x=x-1。但自下而上的工作更好。