删除具有循环条件的行

删除具有循环条件的行

本文介绍了VBA-删除具有循环条件的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须删除必须在VBA excel中等于单元格的所有行.这是我写的:

I have to delete all rows that have to cells equal in VBA excel. Here is what i wrote:

Dim i As Long, j As Long
j = Rows.Count
For i = 1 To j
    If Cells(i, 17).Value = Cells(i, 19).Value Then Rows(i).Delete
Next i

但是此代码不会删除我要查找的每一行.如果有分别符合条件的2行,Excel会跳到另一行并保留该行(这是合乎逻辑的),我不知道如何使此循环适应于返回并删除每条搜索到的行

But this code is not deleting every line I am looking for. If there are 2 rows with matches the conditions respectively, Excel jumps to another one and leaves this (which is logical), I do not know how to adapt this loop to coming back and delete every searched line

推荐答案

我从您的代码中了解的是,由于满足了其中一个条件,因此并没有迭代所有行.我的建议是将其反向循环.使用-1"

What I am understanding from your code is , it is not iterating all the lines since one of the conditions are getting fulfilled .My recommendation is to reverse loop it. Iterate from the last by using "-1"

代替:

For i = 1 To j
    If Cells(i, 17).Value = Cells(i, 19).Value Then Rows(i).Delete
Next i

执行此操作:

For i = j to 1 Step - 1
    If Cells(i, 17).Value = Cells(i, 19).Value Then Rows(i).Delete
Next i

这篇关于VBA-删除具有循环条件的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:27