通过查找列中的列和值,使用VBA删除Excel中的行

时间:2013-12-05 17:44:24

标签: excel-vba vba excel

我正在尝试构建一个宏,它将找到一个标题为“Total Labor”的列,并删除该列中包含“0”的所有行。我正在生成多个报告,“总人工”列将更改位置,这就是我需要查找的原因。到目前为止,我有这个代码,但是当我运行它时没有任何反应。任何帮助表示赞赏。

Sub DeleteRows()
Dim FoundCell As Range
Dim rng As Range
Application.ScreenUpdating = False
Set rng = Worksheets(ActiveSheet.Name).Range("A1:BB100").Find(what:="Total Labor", _
    LookAt:=xlWhole, MatchCase:=False)
Set FoundCell = rng.Find(what:="0")
Do Until FoundCell Is Nothing
    FoundCell.EntireRow.Delete
    Set FoundCell = rng.FindNext
Loop

End Sub

3 个答案:

答案 0 :(得分:0)

怎么样:

Sub DeleteRow()
    Dim colly As Long, killer As Range, nRow As Long
    colly = 0
    For i = 1 To Columns.Count
        If Cells(1, i).Value = "Total Labor" Then
            colly = i
            Exit For
        End If
    Next i
    If colly = 0 Then
        MsgBox "Header not found"
        Exit Sub
    End If
    nRow = Cells(Rows.Count, colly).End(xlUp).Row
    For i = 1 To nRow
        If Cells(i, colly).Value = 0 Then
            If killer Is Nothing Then
                Set killer = Cells(i, colly)
            Else
                Set killer = Union(killer, Cells(i, colly))
            End If
        End If
    Next i
    If killer Is Nothing Then
    Else
        killer.EntireRow.Delete
    End If
End Sub

答案 1 :(得分:0)

您需要复制Excel UI提供的FindAll功能。这是在VBA中实现这一目标的代码列表。将其保存到.bas文件,然后在找到'Total Labor'后在宏中调用它,然后查看从FindAll返回的范围并对其执行.Delete

Sub DeleteRows()

Dim FoundCell As Range
Dim rng As Range

Application.ScreenUpdating = False

Set rng = Worksheets(ActiveSheet.Name).Range("A1:BB100").Find(what:="Total Labor", _
    LookAt:=xlWhole, MatchCase:=False)

If rng Is Nothing Then
    Msgbox "Total Labor Not Found"
Else
    Set SearchRange = rng.EntireColumn
    FindWhat = "0"
    Set FoundCells = FindAll(SearchRange:=SearchRange, _
                            FindWhat:=FindWhat, _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByColumns, _
                            MatchCase:=False, _
                            BeginsWith:=vbNullString, _
                            EndsWith:=vbNullString, _
                            BeginEndCompare:=vbTextCompare)

    If FoundCells Is Nothing Then
        Debug.Print "Value Not Found"
    Else
        For Each FoundCell In FoundCells
            FoundCell.EntireRow.Delete
        Next FoundCell
    End If

End If

End Sub

FindAll 源代码http://www.cpearson.com/excel/findall.aspx

Function FindAll(SearchRange As Range, _
                FindWhat As Variant, _
               Optional LookIn As XlFindLookIn = xlValues, _
                Optional LookAt As XlLookAt = xlWhole, _
                Optional SearchOrder As XlSearchOrder = xlByRows, _
                Optional MatchCase As Boolean = False, _
                Optional BeginsWith As String = vbNullString, _
                Optional EndsWith As String = vbNullString, _
                Optional BeginEndCompare As VbCompareMethod = vbTextCompare) As Range
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' FindAll
' This searches the range specified by SearchRange and returns a Range object
' that contains all the cells in which FindWhat was found. The search parameters to
' this function have the same meaning and effect as they do with the
' Range.Find method. If the value was not found, the function return Nothing. If
' BeginsWith is not an empty string, only those cells that begin with BeginWith
' are included in the result. If EndsWith is not an empty string, only those cells
' that end with EndsWith are included in the result. Note that if a cell contains
' a single word that matches either BeginsWith or EndsWith, it is included in the
' result.  If BeginsWith or EndsWith is not an empty string, the LookAt parameter
' is automatically changed to xlPart. The tests for BeginsWith and EndsWith may be
' case-sensitive by setting BeginEndCompare to vbBinaryCompare. For case-insensitive
' comparisons, set BeginEndCompare to vbTextCompare. If this parameter is omitted,
' it defaults to vbTextCompare. The comparisons for BeginsWith and EndsWith are
' in an OR relationship. That is, if both BeginsWith and EndsWith are provided,
' a match if found if the text begins with BeginsWith OR the text ends with EndsWith.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim FoundCell As Range
Dim FirstFound As Range
Dim LastCell As Range
Dim ResultRange As Range
Dim XLookAt As XlLookAt
Dim Include As Boolean
Dim CompMode As VbCompareMethod
Dim Area As Range
Dim MaxRow As Long
Dim MaxCol As Long
Dim BeginB As Boolean
Dim EndB As Boolean


CompMode = BeginEndCompare
If BeginsWith <> vbNullString Or EndsWith <> vbNullString Then
    XLookAt = xlPart
Else
    XLookAt = LookAt
End If

' this loop in Areas is to find the last cell
' of all the areas. That is, the cell whose row
' and column are greater than or equal to any cell
' in any Area.

For Each Area In SearchRange.Areas
    With Area
        If .Cells(.Cells.Count).Row > MaxRow Then
            MaxRow = .Cells(.Cells.Count).Row
        End If
        If .Cells(.Cells.Count).Column > MaxCol Then
            MaxCol = .Cells(.Cells.Count).Column
        End If
    End With
Next Area
Set LastCell = SearchRange.Worksheet.Cells(MaxRow, MaxCol)

On Error GoTo 0
Set FoundCell = SearchRange.Find(what:=FindWhat, _
        after:=LastCell, _
        LookIn:=LookIn, _
        LookAt:=XLookAt, _
        SearchOrder:=SearchOrder, _
        MatchCase:=MatchCase)

If Not FoundCell Is Nothing Then
    Set FirstFound = FoundCell
    Do Until False ' Loop forever. We'll "Exit Do" when necessary.
        Include = False
        If BeginsWith = vbNullString And EndsWith = vbNullString Then
            Include = True
        Else
            If BeginsWith <> vbNullString Then
                If StrComp(Left(FoundCell.Text, Len(BeginsWith)), BeginsWith, BeginEndCompare) = 0 Then
                    Include = True
                End If
            End If
            If EndsWith <> vbNullString Then
                If StrComp(Right(FoundCell.Text, Len(EndsWith)), EndsWith, BeginEndCompare) = 0 Then
                    Include = True
                End If
            End If
        End If
        If Include = True Then
            If ResultRange Is Nothing Then
                Set ResultRange = FoundCell
            Else
                Set ResultRange = Application.Union(ResultRange, FoundCell)
            End If
        End If
        Set FoundCell = SearchRange.FindNext(after:=FoundCell)
        If (FoundCell Is Nothing) Then
            Exit Do
        End If
        If (FoundCell.Address = FirstFound.Address) Then
            Exit Do
        End If

    Loop
End If

Set FindAll = ResultRange

End Function

答案 2 :(得分:0)

首先:如果您设置Application.ScreenUpdating = False,请确保在子结束前将其重置为True。如果您的宏崩溃,您可能会发现自己无法使用该应用程序,直到您重新启动Excel或运行另一个设置Application.ScreenUpdating = True

的宏

现在,回答您的问题:您的代码存在的问题是代码中定义的rng只会是包含“总人工”的单元格。当您在该范围内搜索值“0”时,行Set FoundCell = rng.Find(what:="0")的计算结果为“Nothing”,因此当您启动do循环时,它符合FoundCell Is Nothing的标准,并立即转到{ {1}}。

这样的事情可以解决问题:

End Sub