Excel Vbamacros根据列标题删除整个行

下面的代码是否可以修改为?

  1. 根据列标题和单元格值,删除多行
  2. 为多列行组合做这个?

例如:列“状态”值“完成”在所有工作表中循环查找任何表示状态的标题,并删除状态已经完整的所有行。

Sub Delete_Rows_Based_On_Header_and_Value () ' ' Delete_Rows_Based_On_Header_and_Value Macro ' ' Declaration Dim a as long Dim w as long Dim vDELCOLs as variant Dim vCOLNDX as variant Dim vDELROWs as variant Dim vROWNDX as variant vDELCOLs = array("status","Status Name","Status Processes") vDELROWs = array("Complete","Completed","Done") with Activeworkbook for w=1 to .worksheets.count with worksheets(w) ' I know this part is to delete columns based on the column name and I am not sure how to modify it to just check column name then delete row based on the value on that column only. for a=lbound(vdelcols) to ubound(vdelcols) vcolndx=application.match(vdelcols(a), .rows(1), 0) if not iserror(vcolndx) then .columns(vcolndx).entirecolumn.delete end if next a end with next w end with 

以下代码将以vDELROWS为数组的数组,并且如果任何值与相应列中的值匹配,则将删除一行。

 Sub Delete_Rows_Based_On_Header_and_Value() ' ' Delete_Rows_Based_On_Header_and_Value Macro ' ' Declaration Dim a As Long Dim w As Long Dim vDELCOLs As Variant Dim vCOLNDX As Variant Dim vDELROWs As Variant Dim vROWNDX As Variant Dim r As Long Dim v As Long vDELCOLs = Array("status", "Status Name", "Status Processes") vDELROWs = Array(Array("Complete", "Pending"), Array("Completed", "Pending"), Array("Done")) With ActiveWorkbook For w = 1 To .Worksheets.Count With Worksheets(w) For a = LBound(vDELCOLs) To UBound(vDELCOLs) vCOLNDX = Application.Match(vDELCOLs(a), .Rows(1), 0) If Not IsError(vCOLNDX) Then For r = .Cells(.Rows.Count, vCOLNDX).End(xlUp).Row To 1 Step -1 For v = LBound(vDELROWs(a)) To UBound(vDELROWs(a)) If .Cells(r, vCOLNDX).Value = vDELROWs(a)(v) Then .Rows(r).EntireRow.Delete Exit For End If Next Next End If Next a End With Next w End With End Sub 

自动filter比循环更有效率

 Sub DeleteRows() Sheet1.Range("a1:c35").AutoFilter Field:=2, Criteria1:="Completed" Sheet1.UsedRange.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete Sheet1.UsedRange.AutoFilter 'repeat for each value End Sub