excel - 根据搜索键删除行VBA

标签 excel vba

我正在尝试使用 VBA 删除在 B 列中找到值“X”的每一行。但是,我遇到了三个问题:

  1. 我无法使用 cells.find 方法让我的 VBA 代码从事件单元格向下移动到下一个单元格 (B3)(请参阅下面的代码)
  2. 我的代码不会删除在 B 列中找到值“X”的整行
  3. B 列中的数据量可能会有所不同:今天可能会以 B10 结束,明天可能会以 B100 结束(请参见下面的屏幕截图)

任何帮助将不胜感激。

Sub RemoveRows()   
    Dim strLookFor As String
    Dim strRow As String

    Worksheets("Sheet1").Range("B2").Activate

    strLookFor = "X"
    strRow = Range("B2").Address

    Do Until ActiveCell.Value = ""        
        MsgBox (ActiveCell.Value)        
        If ActiveCell.Value = strLookFor Then
            Rows.Delete (strRow)
        End If            
        strRow = Cells.Find(what:=strLookFor).Address
    Loop

    MsgBox ("Deleted all rows with value " & strLookFor)    
End Sub

enter image description here

最佳答案

使用自动筛选比范围循环更有效

Sub QuickCull()
Dim ws As Worksheet
Dim rng1 As Range
Set ws = Sheets("Sheet1")
Set rng1 = ws.Range(ws.[b2], ws.Cells(Rows.Count, "B").End(xlUp))
Application.ScreenUpdating = False
With ActiveSheet
        .AutoFilterMode = False
        rng1.AutoFilter Field:=1, Criteria1:="X"
        rng1.Offset(1, 0).EntireRow.Delete
        .AutoFilterMode = False
    End With
Application.ScreenUpdating = True
End Sub

关于excel - 根据搜索键删除行VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13614997/

相关文章:

vba - 删除工作表更改中的单元格并将其上移

excel - 如何删除Excel VBA范围内的列

excel - VSTO - Word 和 Excel 的共享插件以修改功能区

excel - 将另一个工作表中的公式评估为当前工作表

asp.net - C# : Datagrid to Excel . 创建的 Excel 文件中缺少单元格边框

excel - 使用 VBA 根据对动态下拉列表的更新来清除内容

excel - 如何将文本附加到单元格并保持格式?

excel - 单元格为空,但 IsEmpty 不起作用

excel - VBA循环工作表以删除空白列

vba - 如何在PowerPoint 2010中确定音频文件的路径和可用性