【问题标题】:Find value and delete entire row查找值并删除整行
【发布时间】:2016-03-22 22:32:43
【问题描述】:

我正在尝试在列中搜索特定的单元格值,如果找到值,那么我需要删除整行。下面是我到目前为止的代码,我在 Range("X22:X" & lastRow).selection 行上遇到了一个全局错误。

谁能帮忙告诉我哪里出错了。

Dim LResult As String

    Set sht1 = ThisWorkbook.Worksheets("Formatted")
                lastRow = Cells(Rows.Count, x).End(xlUp).Row
                    'MsgBox "Last Row: " & lastRow    'Used to check that the find LastRow worked

            Range("X22:X" & lastRow).Select
            Cells.Find(What:="1: Request").Select
            Selection.EntireRow.Delete

【问题讨论】:

  • Range("X22:X" & lastRow).Select 而不是 Range("X22:X & lastRow").Select
  • 感谢,它现在可以运行,但不会删除具有该单元格值的行
  • find 方法根据方向而不是全部找到第一个或最后一个。为此,您需要向后循环遍历范围或过滤条件并删除可见行。各种方法见HERE。

标签: excel excel-2010 vba


【解决方案1】:

我想添加一个替代解决方案,它可能会更快一些,并且直接来自 Microsoft 自己:https://msdn.microsoft.com/en-us/library/office/ff839746.aspx

Option Explicit

Public Sub RemoveMatchingRows()

Dim rngFound As Range
Dim sht1 As Worksheet

Set sht1 = ThisWorkbook.Worksheets("Formatted")
With sht1.Range("X:X")
    Set rngFound = .Find(What:="1: Request")
    If Not rngFound Is Nothing Then
        While Not rngFound Is Nothing
            sht1.Rows(rngFound.Row).EntireRow.Delete
            Set rngFound = .FindNext
        Wend
    End If
End With

End Sub

【讨论】:

    【解决方案2】:

    不要使用 Find,而是尝试使用 if 循环

    在这里找到解决方案:http://www.rondebruin.nl/win/s4/win001.htm

    我只是根据你提供的信息对其进行了调整

    Sub Loop_Example()
        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long
        Dim CalcMode As Long
        Dim ViewMode As Long
    
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
    
    
        With Sheets("Formatted")
    
            'select the sheet so we can change the window view
            .Select
    
            'If you are in Page Break Preview Or Page Layout view go
            'back to normal view, we do this for speed
            ViewMode = ActiveWindow.View
            ActiveWindow.View = xlNormalView
    
            'Turn off Page Breaks, we do this for speed
            .DisplayPageBreaks = False
    
            'Set the first and last row to loop through
            Firstrow = .UsedRange.Cells(1).Row
            Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
    
            'We loop from Lastrow to Firstrow (bottom to top)
            For Lrow = Lastrow To Firstrow Step -1
    
                'We check the values in the A column in this example
                With .Cells(Lrow, "A")
    
                    If Not IsError(.Value) Then
    
                        If .Value = "1: Request" Then .EntireRow.Delete
                        'This will delete each row with the Value "1: Request"
                        'in Column A, case sensitive.
    
                    End If
    
                End With
    
            Next Lrow
    
        End With
    
        ActiveWindow.View = ViewMode
        With Application
            .ScreenUpdating = True
            .Calculation = CalcMode
        End With
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      在我草拟解决方案时,似乎有很多答案出现了。无论如何我都会发布它。我们只需定义包含我们数据的范围,并利用 for 循环检查第一列上的每个值。

      Public Sub rmv()
      'first define the String that we are looking for
      
      Dim searc As String
      searchTerm = "foo"
      
      'define the sheet we operate on
      
      Set sht1 = ThisWorkbook.Worksheets("Sheet1")
      
      'find last row
      
      lastRow = sht1.Cells(Rows.Count, "B").End(xlUp).Row     '
                      'MsgBox "Last Row: " & lastRow    'Used to check that the find LastRow worked
      
      ' Define the range of data
      
      Dim fRange As Range
      Set fRange = sht1.Range("B2:B" & lastRow)  ' modify this range according to your own needs
      
      ' Loop through each row of the data range
      
      Dim i As Integer
      For i = 1 To fRange.Rows.Count:
      
          If fRange(i, 1) = searchTerm Then
              ' here we check the first cell of the range
              ' this row contains the wanted data => remove entire row
              fRange.Rows(i).EntireRow.Delete
          End If
       Next i
      
       End Sub
      

      【讨论】:

        猜你喜欢
        • 2020-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-27
        • 1970-01-01
        • 2018-09-23
        相关资源
        最近更新 更多