如何基于条件删除Excel工作表中的行

时间:2011-10-19 13:02:20

标签: excel excel-vba vba

我有一个excel工作簿,在A列的工作表1中,如果该列的值= ERR我希望它被删除(整行),这怎么可能?

PS:请记住,我之前从未使用过VBA或Macros,因此非常感谢详细介绍。

4 个答案:

答案 0 :(得分:2)

  sub delete_err_rows()
      Dim Wbk as Excel.workbook  'create excel workbook object
      Dim Wsh as worksheet       ' create excel worksheet object 
      Dim Last_row as long
      Dim i as long
      Set Wbk = Thisworkbook ' im using thisworkbook, assuming current workbook
                             ' if you want any other workbook just give the name 
                             ' in invited comma as "workbook_name"
      Set Wsh ="sheetname"   ' give the sheet name here 
      Wbk.Wsh.activate
     ' it means Thisworkbook.sheets("sheetname").activate
     ' here the sheetname of thisworkbook is activated
     ' or if you want looping between sheets use thisworkbook.sheets(i).activate
     ' put it in loop , to loop through the worksheets
     ' use thisworkbook.worksheets.count to find number of sheets in workbook
     Last_row = ActiveSheet.Cells(Rows.count, 1).End(xlUp).Row 'to find the lastrow of the activated sheet
          For i = lastrow To 1 step -1
             if activesheet.cells(i,"A").value = "yourDesiredvalue"
                  activesheet.cells(i,"A").select  ' select the row
                  selection.entirerow.delete       ' now delete the entire row
             end if
          Next i
   end sub

请注意,使用活动表格执行的任何操作都会在当前激活的工作表中受到影响

正如你所说的那样,你为什么不记录一个宏并查看,这是通过查看背景代码来自动化你的过程的最好方法

只需找到工作表上的宏选项卡,然后单击记录新宏,然后选择任何一行并执行您想要执行的操作,例如删除整行,只需删除整行,现在返回宏选项卡然后单击停止录制。

现在单击alt + F11,这将带您进入VBA编辑器,在vba项目资源管理器字段中找到一些工作表和模块,如果您没有找到它使用VBA编辑器的视图选项卡搜索它,现在点击module1并看到录制的宏,你会发现类似这样的东西

        selection.entirerow.delete

我希望我能帮助你一点,如果你需要更多帮助,请告诉我,谢谢

答案 1 :(得分:2)

手动或使用VBA(如下所示)使用自动过滤器是一种非常有效的删除行的方法

以下代码

  1. 适用于整个使用范围,即将处理空白
  2. 可以通过更改strSheets = Array(1, 4)轻松访问其他工作表。即此代码目前在第一张和第四张上运行

     Option Explicit
    
    
    Sub KillErr()
    Dim ws As Worksheet
    Dim lRow As Long
    Dim lngCol As Long
    Dim rng1 As Range
    Dim strSheets()
    Dim strws As Variant
    strSheets = Array(1, 4)
    For Each strws In strSheets
        Set ws = Sheets(strws)
        lRow = ws.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
        lngCol = ws.Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
        Application.ScreenUpdating = False
        ws.Rows(1).Insert
        Set rng1 = ws.Range(ws.Cells(1, lngCol), ws.Cells(lRow + 1, lngCol))
        With rng1.Offset(0, 1)
            .FormulaR1C1 = "=RC1=""ERR"""
            .AutoFilter Field:=1, Criteria1:="TRUE"
            .EntireRow.Delete
            On Error Resume Next
            .EntireColumn.Delete
            On Error GoTo 0
        End With
    Next
    Application.ScreenUpdating = True
    End Sub
    

答案 2 :(得分:2)

最快的方法:

Sub DeleteUsingAutoFilter()

    Application.ScreenUpdating = False

    With ActiveSheet
        .AutoFilterMode = False

        .Columns("A").AutoFilter Field:=1, Criteria1:="ERR"

        .AutoFilter.Range.Offset(1, 0).EntireRow.Delete        

        .AutoFilterMode = False
    End With

    Application.ScreenUpdating = True

End Sub

第二快的方法(对此也有很多变化):

Sub DeleteWithFind()
    Dim rFound As Range, rDelete As Range
    Dim sAddress As String

    Application.ScreenUpdating = False

    With Columns("A")
        Set rFound = .Find(What:="ERR", After:=.Resize(1, 1), SearchOrder:=xlByRows)

        If Not rFound Is Nothing Then
            Set rDelete = rFound                
            Do
                Set rDelete = Union(rDelete, rFound)
                Set rFound = .FindNext(rFound)
            Loop While rFound.Row > rDelete.Row                
        End If

        If Not rDelete Is Nothing Then rDelete.EntireRow.Delete

    End With

    Application.ScreenUpdating = True

End Sub

多张纸的自动过滤方法:

Sub DeleteUsingAutoFilter()
    Dim vSheets As Variant
    Dim wsLoop As Worksheet

    Application.ScreenUpdating = False

    '// Define worksheet names here
    vSheets = Array("Sheet1", "Sheet2")

    For Each wsLoop In Sheets(vSheets)

         With wsLoop
             .AutoFilterMode = False

             .Columns("A").AutoFilter Field:=1, Criteria1:="ERR"

             .AutoFilter.Range.Offset(1, 0).EntireRow.Delete

             .AutoFilterMode = False
         End With

    Next wsLoop

    Application.ScreenUpdating = True

End Sub

答案 3 :(得分:0)

假设A列中的单元格中始终存在值,并且数据位于第一张表格中,那么这样的内容应该可以达到您想要的效果:

Sub deleteErrRows()
    Dim rowIdx As Integer
    rowIdx = 1

    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets(1)

    While ws.Cells(rowIdx, 1).Value <> ""
        If ws.Cells(rowIdx, 1).Value = "ERR" Then
            ws.Cells(rowIdx, 1).EntireRow.Delete
        Else
            rowIdx = rowIdx + 1
        End If
    Wend
End Sub