如何根据列的值删除行?

huangapple go评论72阅读模式
英文:

How to delete rows based on columns' value?

问题

我正在尝试根据一列的值删除行!但工作表中范围的列长度是动态且较大的。

例如,
如果列C的值小于或等于0,那么该行将被删除。

只应该给我:

A B C
1 SAM 100
1 HAWK 100
英文:

I am trying to delete rows based on one columns value! But the column length of the range in the worksheet is dynamic and large.

For example,
If Col C has value less than or equal to 0 that row gets deleted

A B C
1 SAM 100
1 SAM 0
1 BRI -100
1 HAWK 100

It should only give me :

A B C
1 SAM 100
1 HAWK 100

答案1

得分: 3

根据我的评论所说,请尝试以下代码:

Sub test()
Dim LR As Long
Dim i As Long

LR = Range("A" & Rows.Count).End(xlUp).Row '获取最后一个非空行号

For i = LR To 1 Step -1 '从LR开始向后,直到第1行
    If Range("C" & i).Value <= 0 Then Range("C" & i).EntireRow.Delete
Next i

End Sub

代码执行前:

如何根据列的值删除行?

代码执行后:

如何根据列的值删除行?

英文:

as said in my comment,try this:

Sub test()
Dim LR As Long
Dim i As Long

LR = Range(&quot;A&quot; &amp; Rows.Count).End(xlUp).Row &#39;get last non blank row number

For i = LR To 1 Step -1 &#39;go backwards starting at LR until row 1
    If Range(&quot;C&quot; &amp; i).Value &lt;= 0 Then Range(&quot;C&quot; &amp; i).EntireRow.Delete
Next i


End Sub

Before code:

如何根据列的值删除行?

After code executed:

如何根据列的值删除行?

huangapple
  • 本文由 发表于 2023年6月1日 20:57:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定