如何基于标准删除Excel工作表中的行

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

PS:请记住,我从来没有使用过VBA或macros,所以详细的描述非常感谢。

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

下面的代码

  1. 在整个使用的范围内工作,即将处理空白
  2. 通过改变strSheets = Array(1, 4)可以很容易的将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 
  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 

请注意,使用activesheet进行的任何操作都将在当前激活的工作表上受到影响

正如你所说的你的开头,为什么不logging一个macros,并检查出来,这是通过看到后台代码自动化你的过程的最大方法

只要find工作表上的macros选项卡,然后点击logging新的macros,然后select任何一行,做你想做的事情,说删除整个行,只是删除整个行,现在回到macros选项卡,然后单击停止录音。

现在点击alt + F11,这会把你带到VBA编辑器,在vba项目资源pipe理器字段中find一些工作表和模块,如果你没有find它,用VBA编辑器的视图标签进行search,现在点击module1,看看录制的macros,你会发现这样的东西

  selection.entirerow.delete 

我希望我帮了你一些忙,如果你需要更多的帮助,请告诉我,谢谢

最快的方法:

 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 

假设列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