简体   繁体   English

我如何在VBA中编写一个循环,如果一个范围内的单元格及其旁边的单元格都等于一个常数,它将删除一行?

[英]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.

相关问题 我如何使Excel VBA移动到for循环中的下一个单元格? - how do i make excel vba move to the next cell in a for loop? 如何遍历单元格范围并在vba中用单元格值命名单元格? - How do I loop through a cell range and name the cells with cell value in vba? Python-如何使用for循环将数据写入单元格,然后移至下一行 - Python - How to use a for loop to write data to a cell, then move to the next row EXCEL VBA:如果cell.value =“ WORD”在范围内,则如何处理下一个单元格(同一行)的值 - EXCEL VBA: How to manupulate next cell's (same row) value if cell.value=“WORD” in a range VBA如何获取单元格值作为模块级别常量? - VBA How do I get a cell value as module level constant? Excel VBA:如何将文本添加到特定列中的空白单元格,然后循环到下一个空白单元格并添加文本? - Excel VBA: How do I add text to a blank cell in a specific column then loop to the next blank cell and add text? VBA循环:如果单元格包含特定值,则删除包含该值的行以及下面的下3行 - VBA Loop: if cell contains specific value then delete the row containing the value and the next 3 rows below Excel 宏/VBA - 如果一列中的单元格为空,如何删除整行? - Excel Macro / VBA - How do I delete an entire row if a cell in one column is blank? 如果单元格以某个单词结尾,如何删除整行? VBA - If a cell ends in a certain word, how do I delete the entire row? VBA 联合范围VBA上的下一个单元格 - Next cell on Union range VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM