[英]VBA Trying to loop through a column and delete the entire row if they contain a value
I'm not very good at loops.我不太擅长循环。
I'm trying to use VBA to loop through a column to look for any value, and then delete the entire row if it doesn't find anything (It's essentially a way of deleting rows of data that I've marked (or unmarked in this case)).我正在尝试使用 VBA 循环遍历列以查找任何值,然后如果找不到任何值,则删除整行(这本质上是一种删除我已标记(或未标记)的数据行的方法这个案例))。
I've tried various things.我尝试过各种各样的事情。 My most recent attempt is below but its just deleting every row regardless of whether that cell has a value or not.
我最近的尝试如下,但它只是删除每一行,无论该单元格是否有值。 Any suggestions?
有什么建议么?
Dim i as Long
For i = 1 To 50
If Cells(i, 1).Value = "" Then
Selection.EntireRow.Delete
Else
i = i + 1
End If
Next i
End Sub
There are several issues here:这里有几个问题:
When deleting rows in a loop work backwards, if going forward your row number changes as you delete.在循环中删除行时向后工作,如果向前移动,您的行号会随着您的删除而改变。
There is no need to increment variable "i" next i already does this接下来不需要增加变量“i”我已经这样做了
Use the Worksheet object to delete the row rather than Selection使用工作表 object 删除行而不是选择
I would rewrite like this:我会这样重写:
Sub delete()
Dim i As Long
For i = 50 To 1 Step -1
If Cells(i, 1).Value = "" Then
Worksheets("Sheet1").Rows(i).EntireRow.delete
End If
Next i
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.