查找值并删除整行

时间:2016-03-22 13:32:12

标签: excel excel-vba excel-2010 vba

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

任何人都可以帮忙告诉我哪里出错了。

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

3 个答案:

答案 0 :(得分:1)

尝试使用if循环

,而不是使用Find

此处找到解决方案: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

答案 1 :(得分: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 :(得分:0)

似乎在我草绘解决方案时收到了许多答案。无论如何我会发布它。我们只需定义包含数据的范围,并利用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