简体   繁体   English

Excel VBA:如果在另一个范围内找到单元格值,则循环删除行

[英]Excel VBA: Loop to delete rows if cell value is found within another range

I am somewhat novice to VBA.我对 VBA 有点陌生。 I have a column of around 500 names.我有一列大约 500 个名字。 In another sheet I have a shorter column of names all found within the longer.在另一张表中,我有一个较短的名称列,所有名称都在较长的范围内。

I am trying to loop through the longer column and delete all the rows where the cell value is not found within the shorter.我正在尝试遍历较长的列并删除在较短的列中未找到单元格值的所有行。 My code works but only deletes rows in increments and needs to be run about 10 times to complete the task.我的代码可以工作,但只会以增量方式删除行,并且需要运行大约 10 次才能完成任务。

I know loops involving deletion can be tricky.我知道涉及删除的循环可能很棘手。 Any help would be appreciated!任何帮助,将不胜感激!

 Sub FindMatches()
    Application.ScreenUpdating = False
    
   
    Dim var As Variant, iSheet As Integer, iRow As Long, iRowL As Long, bln As Boolean
    
      
       iRowL = Cells(Rows.Count, 1).End(xlUp).Row
      
       For iRow = 1 To iRowL
      

          If Not IsEmpty(Cells(iRow, 1)) Then
             For iSheet = ActiveSheet.Index + 1 To Worksheets.Count
                bln = False
                var = Application.Match(Cells(iRow, 1).Value, Worksheets(iSheet).Columns(1), 0)
                
                If Not IsError(var) Then
                   bln = True
                   Exit For
                End If
             Next iSheet
          End If
          
       
          If bln = False Then
        
             Cells(iRow, 1).EntireRow.Delete
             Else
             Cells(iRow, 1).Font.Bold = True

           End If
       
       Next iRow
    Application.ScreenUpdating = True
End Sub

I've run into this too, where you delete a row, and it throws off your count, effectively skipping a row, since row 100 for instance, has now become row 99.我也遇到过这种情况,你删除了一行,它会抛出你的计数,有效地跳过一行,例如,因为第 100 行现在变成了第 99 行。

I think if within your if argument where you delete a row, just write a line after you do that for iRow = iRow - 1, then it will go back a row that you just deleted and carry on from there.我认为,如果在您删除一行的 if 参数中,只需在执行 iRow = iRow - 1 之后写一行,那么它将 go 返回您刚刚删除的一行并从那里继续。

I recommend a different approach and start by mapping the rows that you want to delete and after the loop finishes just delete the union of rows (example below) another approach will be to use a reverse loop so starting from the bottom and work your way up to first record and if you don't find what your looking for delete the row.我推荐一种不同的方法,首先映射要删除的行,然后在循环完成后删除行的联合(下面的示例)另一种方法是使用反向循环,因此从底部开始向上工作首先记录,如果您没有找到您要查找的内容,请删除该行。 This way you don't need to account for the rows that offset due to delete.这样您就不需要考虑由于删除而偏移的行。

Sub DeleteBlankRows()
Dim delRange As Range
Dim lrow As Long, i As Long
Dim WS As Worksheet

Set WS = ActiveSheet

    With WS
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row

 '--> Delete All rows where Cell A and Cell B are empty
        For i = 6 To lrow
            
            If Len(Trim(.Range("A" & i).value)) = 0 Or Len(Trim(.Range("B" & i).value)) = 0 Then
                If delRange Is Nothing Then
                    Set delRange = .Rows(i)
                Else
                    Set delRange = Union(delRange, .Rows(i))
                End If
            End If
        Next i

        If Not delRange Is Nothing Then delRange.Delete
        
        Set delRange = Nothing
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM