[英]How do I write a loop in VBA that will delete a row if a cell in a range and the cell next to it both equal a constant?
[This is in Excel 2007] [这是在Excel 2007中]
In other words, the loop will cycle through all the active cells in a one-column range (rngAddressName) and, if the cell in the range AND the cell directly to the left of it contain the string "#N/A", then it will delete that row. 换句话说,该循环将循环遍历一个列范围(rngAddressName)中的所有活动单元格,并且,如果范围AND紧邻其左侧的单元格中包含字符串“#N / A”,则它将删除该行。
Unfortunately, nothing I have tried has had any actual effect. 不幸的是,我尝试过的一切都没有任何实际效果。 Here is my best go at it:
这是我最好的选择:
i = 1
For counter = 1 To rngSC2A.Rows.Count
Contents = rngSC2A.Cells(i).Value
If Contents = "#N/A" Then
If rngAddressName.Cells(i).CellOffset(0, -1).Value = "#N/A" Then
rngAddressName.Cells(i).EntireRow.Delete
Else
End If
Else
i = i + 1
End If
Next
But this doesn't seem to find any rows with the conditions satisfied (even though such rows exist in the worksheet). 但这似乎找不到满足条件的行(即使这些行存在于工作表中)。 I think it might have something to do with the fact that I am looking in the Cell.Value, but I am not sure.
我认为这可能与我正在查看Cell.Value中的事实有关,但我不确定。
You can autofilter your range, delete any rows that meet your criteria, then turn the autofilter off. 您可以自动过滤范围,删除符合条件的所有行,然后关闭自动过滤功能。 This is a much more efficient approach than looping.
这比循环更有效。
The example below works on columns A and B in Sheet1. 下面的示例适用于Sheet1中的A和B列。 Modify the variables to reference the range and sheet in your workbook.
修改变量以引用工作簿中的范围和工作表。
Sub DeleteDoubleNA()
Dim ws As Worksheet
Dim rng As Range
Dim lastRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rng = ws.Range("A1:B" & lastRow)
' filter and delete all but header row
With rng
.AutoFilter field:=1, Criteria1:="#N/A"
.AutoFilter field:=2, Criteria1:="#N/A"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
' turn off the filters
ws.AutoFilterMode = False
End Sub
This is a different take on on the excellent answer posted by @Jon Crowell. 这与@Jon Crowell发布的出色答案截然不同。
If you use an Excel table, you can use the table's ListObject
to get the data range which automatically excludes the header and footer rows. 如果使用Excel表,则可以使用表的
ListObject
获取数据范围,该范围自动排除页眉和页脚行。
This avoids the sometimes incorrect calculation search for a last row. 这样避免了有时对最后一行进行错误的计算搜索。
You also want to clear any pre-existing filters on the data so that you don't overlook any rows. 您还希望清除数据上的所有现有过滤器,以免忽略任何行。
Dim myTable As Object
Set myTable = ActiveSheet.ListObjects(1) ' Works if worksheet contains only one table
' Clear pre-existing filters on the table
myTable.AutoFilter.ShowAllData
' Filter the table
With myTable.DataBodyRange
.AutoFilter field:=1, Criteria1:="#N/A"
.AutoFilter field:=2, Criteria1:="#N/A"
End With
' Delete visible cells in the filtered table
myTable.DataBodyRange.SpecialCells(xlCellTypeVisible).EntireRow.Delete
' Clear filters on the table
myTable.AutoFilter.ShowAllData
The (1)
in ListObjects(1)
is the first (in my case only) table in the worksheet. 在
(1)
在ListObjects(1)
是第一个(对我来说唯一的)工作表。 DataBodyRange
refers to the data range of that table excluding header and footer rows. DataBodyRange
引用该表的数据范围,不包括页眉和页脚行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.