【问题标题】:Delete blank rows based on multiple criteria根据多个条件删除空白行
【发布时间】:2015-06-18 00:08:49
【问题描述】:

我正在尝试根据分布在多个列中的多个条件删除行。本质上,我在 A 列中有一个名称列表,在第 1 行中有一个计划列表。

问题是,有时有一个计划 1,有时有一个计划 2,有时两者都有,如上图所示。这些计划还会更改它们所在的列。

我正在尝试编写一些 vba 来查看标题中是否存在这些计划中的任何一个,如果其中一个或两个都存在,请删除其中一个人在两者中都是空白的行。在上图中,需要删除带有“Barry Bonds”的行。

到目前为止,我有删除一列中的空白的方法,但我不知道如何删除一列以上的内容,或者在计划名称之间切换(即计划 1 或计划 2)。

Sub Delete_non_used_Loc()

Dim ws As Worksheet
Dim ws2 As Worksheet
Dim Acell As Range
Dim rng As Range
Dim col As Long
Dim Lastrow As Long
Dim colName As String

'Value for deleting blanks
Dim DelTerm As Range
Set ws = Sheets("hello")
'This statement finds the Plan in the  column in the carrier report.
With ws
        Set Acell = .Range("A1:Z2").Find(What:="Plan 1", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
'If the plan column is found, then:
If Not Acell Is Nothing Then
    col = Acell.Column
    colName = Split(.Cells(, col).address, "$")(1)
    Lastrow = .Range(colName & .Rows.Count).End(xlUp).Row
    'This is the range of rows in the column specified (Plan 1).
    Set rng = .Range(colName & "2:" & colName & Lastrow)
'Finds and deletes terminated employees by searching for blanks via the Plan 1 column.
 Do
    Set DelTerm = rng.Find("", LookIn:=xlValues) 'Finds the blanks.
    If Not DelTerm Is Nothing Then DelTerm.EntireRow.Delete 'Deletes the row     where blanks are found.
Loop While Not DelTerm Is Nothing
Else
    MsgBox "Could not delete blank employees!"
End If
End With

End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    更新答案 更具挑战性,但绝对仍然可行!为了维护这些“随机数据字段”,我们将利用一个 Collection 对象来保存所有感兴趣的列号(即任何包含“计划”一词的列号)。

    通过遍历第一行并构建Collection,我们稍后可以简单地遍历Collection并只查看重要的单元格。

    下面被大量注释的代码应该可以解决问题。

    Option Explicit
    Public Sub DeleteBlankRowsRev2()
    
        Dim wks As Worksheet
        Dim lngLastRow As Long, lngLastCol As Long, lngIdx As Long
        Dim varColItem As Variant
        Dim blnAllBlank As Boolean
        Dim col As Collection
        Set col = New Collection
    
        'First things first: we identify our basic variables
        Set wks = ThisWorkbook.Worksheets("hello")
    
        With wks
            'Now that our sheet is defined, we'll find the last row and last column
            lngLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                                     SearchOrder:=xlByRows, _
                                     SearchDirection:=xlPrevious).Row
            lngLastCol = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                                     SearchOrder:=xlByColumns, _
                                     SearchDirection:=xlPrevious).Column
    
            'Awesome! With the last row and column in our pocket, we now
            'know a lot about the data range we will be checking for blanks
    
            'Here we loop through the first row, adding any column number
            'that contains the word "plan"
            For lngIdx = 1 To lngLastCol
                If InStr(UCase(CStr(.Cells(1, lngIdx))), "PLAN") Then
                    col.Add lngIdx
                End If
            Next lngIdx
    
            'Since we need to delete rows, we start from the bottom and move up
            For lngIdx = lngLastRow To 1 Step -1
    
                'Start by setting a flag to immediately stop checking
                'if a cell is NOT blank
                blnAllBlank = True
    
                'Loop through all the column numbers contained in our collection
                For Each varColItem In col
    
                    'If the cell is NOT blank, change the flag to False
                    'then exit this For loop
                    If .Cells(lngIdx, CInt(varColItem)) <> "" Then
                        blnAllBlank = False
                    End If
                    If Not blnAllBlank Then Exit For
                Next varColItem
    
                'Delete the row if the blnBlank variable is True
                If blnAllBlank Then
                    .Rows(lngIdx).Delete
                End If
    
            Next lngIdx
        End With
    
        'That's it -- we're done!
        MsgBox "Script complete!"
    
    End Sub
    

    原答案 下面的大量注释代码应该可以解决问题。

    简而言之,我们:

    1. 确定计划数据的范围
    2. 循环遍历范围 BACKWARDS
    3. 对于每一行,如果上面 #1 中定义的空间中的每个单元格都是空的,则删除该行

    别忘了庆祝!

    Option Explicit
    Public Sub DeleteBlankRows()
    
        Dim wks As Worksheet
        Dim lngLastRow As Long, lngLastCol As Long, lngIdx As Long, _
            lngColCounter As Long
        Dim blnAllBlank As Boolean
    
        'First things first: we identify our basic variables
        Set wks = ThisWorkbook.Worksheets("hello")
    
        With wks
            'Now that our sheet is defined, we'll find the last row and last column
            lngLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                                     SearchOrder:=xlByRows, _
                                     SearchDirection:=xlPrevious).Row
            lngLastCol = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                                     SearchOrder:=xlByColumns, _
                                     SearchDirection:=xlPrevious).Column
    
            'Awesome! With the last row and column in our pocket, we can
            'loop over the full range (whether it only has 2 columns or 4)
    
            'Since we need to delete rows, we start from the bottom and move up
            For lngIdx = lngLastRow To 1 Step -1
    
                'Start by setting a flag to immediately stop checking
                'if a cell is NOT blank and initializing the column counter
                blnAllBlank = True
                lngColCounter = 2
    
                'Check cells from left to right while the flag is True
                'and the we are within the farthest-right column
                While blnAllBlank And lngColCounter <= lngLastCol
    
                    'If the cell is NOT blank, trip the flag and exit the loop
                    If .Cells(lngIdx, lngColCounter) <> "" Then
                        blnAllBlank = False
                    Else
                        lngColCounter = lngColCounter + 1
                    End If
    
                Wend
    
                'Delete the row if the blnBlank variable is True
                If blnAllBlank Then
                    .Rows(lngIdx).Delete
                End If
    
            Next lngIdx
        End With
    
        'That's it -- we're done!
        MsgBox "Script complete!"
    
    End Sub
    

    【讨论】:

    • 嗨,丹,非常感谢您的回答,我很想庆祝一下。但是,我应该指定“计划”列不一定是工作簿中的唯一列。整个工作簿中可能会有一些随机的列。我已经修改了图片以显示它的外观。我知道标题将始终被称为“计划 1”、“计划 2”等,但查找最后一列将不起作用,因为这些列中的数据不是我需要过滤的。我想我可以删除未使用的列...
    • 嘿@NLourme——那些“随机数据”列需要保留,还是可以删除它们?如果可以删除它们,则可以使用快速循环修改上述解决方案,该循环遍历列(当然是向后)并删除不包含字符串 Plan 的列
    • 嗨@Dan Wagner,我可以删除它们,但我不想这样做。我在这些列上运行其他宏和测试,所以我需要在之后重新添加它们。虽然我有一个是在数组中设置值“Plan 1”、“Plan 2”等,然后循环遍历这些值。不确定您是否认为这是否可行?
    • 我上面的答案已经更新了一个额外的解决方案——应该可以工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2022-11-18
    • 2021-02-24
    • 2018-06-12
    相关资源
    最近更新 更多