Excel Vba宏基于列标题删除整行

时间:2017-03-15 22:09:54

标签: excel vba excel-vba

以下代码是否可以修改为

  1. 根据列标题和单元格值删除多行,
  2. 为多列行组合执行此操作?
  3. 示例:列"状态"价值"完成" 遍历所有工作表并查找任何表示状态的标题并删除状态已完成的所有行?

    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
    

2 个答案:

答案 0 :(得分:1)

以下代码将数组数组作为 public class WrappedWebDriver { public WebDriver driver; public WrappedWebDriver(WebDriver driver){ this.driver = driver } public WebElement find(By by){ //your customization code return driver.findElement(by); } public void setText(By by, String text){ //your customization code driver.findElement(by).sendKeys(text) } public void performCustomAction(){ //your customization code } } ,如果任何值与相应列中的值匹配,则会删除一行。

vDELROWS

答案 1 :(得分:0)

自动过滤比循环更有效

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