VBA 循环中的代码永无止境.如何解决这个问题? [英] Code in VBA loops and never ends. How to fix this?

查看:29
本文介绍了VBA 循环中的代码永无止境.如何解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行此代码以删除 > -100 的行.然而,它不断循环,永不停止.

I run this code to delete rows which have have > -100. However it keeps looping and never stops.

我在这里遗漏了什么?

For i = 2 To 500
If Worksheets("Sales").Cells(i, 3).Value > -100 Then
   Worksheets("Sales").Cells(i, 3).EntireRow.Delete
   i = i - 1
End If
Next i

推荐答案

也许您可以合并行并立即删除它们?像这样的东西(未经测试).

Maybe you could union the rows and delete them at once? Something like this (untested).

Dim myRow As Range
Dim toDelete As Range

For i = 2 To 500
    If Worksheets("Sales").Cells(i, 3).Value > -100 Then
       Set myRow = Worksheets("Sales").Rows(i)
       If toDelete Is Nothing Then
            Set toDelete = myRow
        Else
            Set toDelete = Union(toDelete, myRow)
        End If
    End If
Next i

If Not toDelete Is Nothing Then _
    toDelete.Delete

这篇关于VBA 循环中的代码永无止境.如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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