如何反转For循环 [英] How to reverse a For loop

查看:248
本文介绍了如何反转For循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的宏有问题。它删除完整的某些条件的行。但是当连续几行满足这些条件时,会发生错误。当行被删除时,其他行被向上移动,如果即使行(2:2)和行(3:3)fullfil标准,则行(2:2)被删除,行(3:3)被向上移动,所以它成为行(2:2),并且For循环到另一行(第三个)。结果行以前是行(3:3)现在是行(2:2)被省略而不被删除。

为了处理这个话题,我认为这是足够的反向For循环,所以它不会从上到下,但从底部到顶部。 Te resulat将是对一些行的双重检查,但不会省略任何行。

问题是我不知道如何修复For循环。我试图改变'for x = startrow To endrow'to'For x = endrow To startrow',但是没有帮助。

这里是代码:

  Sub Repurchase_upload()

Dim Worksheet As Worksheets
startrow = Worksheets(GUTS)。 )
endrow = Worksheets(GUTS)。单元格(11,1)

对于x = startrow要endrow'我试图将此行更改为:'for x = endrow To startrow',但它没有帮助
如果Cells(x,A)。值<> AA和细胞(x,A)。值<> AB和细胞(x,A)。值<> AC和Cells(x,A)。值<> AD和Cells(x,A)。值<> AE和细胞(x,A)。 AH和细胞(x,A)。值<> AI和单元格(x,A)。值<> AF和细胞(x,A)。值<> AG然后
单元格(x,A)。EntireRow.Delete

如果



下一个
End Sub

提前谢谢大家,

最好的问候,

Artur Rutkowski

解决方案

如果您循环删除行,则需要从底部和工作:

 对于x = endrow要开始步骤-1 
/ pre>

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


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:

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

解决方案

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.

这篇关于如何反转For循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆