英文:
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("A" & Rows.Count).End(xlUp).Row 'get last non blank row number
For i = LR To 1 Step -1 'go backwards starting at LR until row 1
If Range("C" & i).Value <= 0 Then Range("C" & i).EntireRow.Delete
Next i
End Sub
Before code:
After code executed:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论