简体   繁体   English

如果任一列中的一个或多个单元格为空白,则删除整行

[英]Delete entire row if one or more cells are blank in any column

I am trying to iterate through rows and delete a row if there is a blank cell in the row, on a button click, regardless of what column it is in.我正在尝试遍历行并删除一行,如果该行中有一个空白单元格,则单击按钮,无论它位于哪一列。

I have 151 rows and Columns A to L.我有 151 行和 A 到 L 列。

When I run the code all my rows disappear.当我运行代码时,我的所有行都消失了。

Sub Button3_Click()
Dim lRow As Long
Dim iCntr As Long
Dim col As Integer
col = 1
lRow = 151
For iCntr = lRow To 1 Step -1
    If Trim(Cells(iCntr, col)) = "" Then
        Rows(iCntr).Delete
    End If
    col = col + 1
Next
    
End Sub

You need to loop across a fixed set of columns too - at the moment you appear to be just examining just one one cell per row - the col = col + 1 means you examine a diagonal stripe of cells, different column on each row.您还需要遍历一组固定的列 - 目前您似乎只检查每行一个单元格 - col = col + 1意味着您检查单元格的对角条纹,每行上的不同列。

Anyway it's better to collect all deletion rows and delete at the end of the process, so that you're not altering the space you're examining.无论如何,最好收集所有删除行并在过程结束时删除,这样您就不会改变您正在检查的空间。

Sub Button3_Click()
Dim irx As Long
Dim ARow As Range
Dim ACell As Range
Dim DelSet As Range

Set DelSet = Nothing
For irx = 1 To 151
    Set ARow = Range(Cells(irx, 1), Cells(irx, 12))
    For Each ACell In ARow
        If Trim(ACell.Value) = "" Then
            If DelSet Is Nothing Then
                Set DelSet = ACell.EntireRow
            Else
                Set DelSet = Union(DelSet, ACell.EntireRow)
            End If
            Exit For
        End If
    Next ACell
Next irx

If Not DelSet Is Nothing Then
    DelSet.Delete
End If

End Sub

To see what this is doing, without actually deleting anything, you could change the DelSet.Delete into DelSet.Select and it will show you what the allocated region for deletion is.要查看这是在做什么,而不实际删除任何内容,您可以将DelSet.Delete更改为DelSet.Select ,它将显示分配的删除区域是什么。

I recognize from your comment that this use of Range object variables might be new to you, but definitely worth learning.我从您的评论中认识到,使用Range object 变量对您来说可能是新的,但绝对值得学习。

Delete Rows Containing At Least One Blank Cell删除包含至少一个空白单元格的行

  • If any of the cells in a row of the 'given' range ( rg ) is blank ( empty , ="" , ' etc.), then the row range ( rrg ) will be combined (using Union ) into the Delete Range ( drg ).如果“给定”范围 ( rg ) 行中的任何单元格为空白 ( empty=""'等),则行范围 ( rrg ) 将 (使用Union ) 组合到删除范围 ( drg )。 Finally, the entire rows of the Delete Range will be deleted.最后,删除范围的整行都将被删除。
  • Your use of Trim may indicate that you also want to delete rows containing spaces.您使用Trim可能表明您还想删除包含空格的行。 This is not covered by this solution (a cell is not blank if it contains a space).此解决方案未涵盖这一点(如果单元格包含空格,则单元格不是空白)。

A Quick Fix快速修复

Option Explicit

Sub Button3_Click()
    
    Const Cols As String = "A:L"
    Const First As Long = 1
    Const Last As Long = 151
        
    Dim rg As Range: Set rg = Columns(Cols).Rows(First & ":" & Last)
    
    Dim drg As Range
    Dim rrg As Range
    
    For Each rrg In rg.Rows
        If Application.CountBlank(rrg) > 0 Then
            If drg Is Nothing Then
                Set drg = rrg
            Else
                Set drg = Union(drg, rrg)
            End If
        End If
    Next rrg
    
    If Not drg Is Nothing Then
        drg.EntireRow.Delete
    End If
    
End Sub

Your Idea Fixed你的想法固定

Sub Button3_Click()
    
    Const fRow As Long = 1
    Const lRow As Long = 151
    Const fCol As Long = 1 ' ("A")
    Const lCol As Long = 12 ' ("L")
    
    Dim r As Long, c As Long
    
    For r = lRow To fRow Step -1
        For c = fCol To lCol
            If Trim(Cells(r, c).Value) = "" Then
                Rows(r).Delete
                Exit For ' found and deleted: don't loop the columns any more
            End If
        Next c
    Next r
    
End Sub

you need to select the row using the sintaxis: Eg Rows(nrow:nrow).select --> Rows(4:4).select你需要 select 使用 sintaxis 的行:例如 Rows(nrow:nrow).select --> Rows(4:4).Z99938282F04071859941E18F16EF

But you are counting in countdown, I guess you will need to count how many rows you have deleted.但是你正在倒计时,我想你需要计算你删除了多少行。 to select the right row.到 select 右行。

Good luck祝你好运

Sub Button3_Click()
    Dim lRow As Long
    Dim iCntr As Long
    Dim col As Integer
    col = 1
    lRow = 151
    For iCntr = lRow To 1 Step -1
        For col = 1 To 12 '12 is L
            If Trim(Cells(iCntr, col)) = "" Then
                Rows(iCntr & ":" & iCntr).Select
                Selection.Delete Shift:=xlUp
                Exit For ' if found 1 blank, get out afeter delete
                'After delet check wich cell you need to be stan on
            End If
        Next col
    Next
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel:如果任何列中的单元格为空,则删除整行(长数据集) - Excel: Delete entire row if cell in any column is blank (Long Dataset) 清除仅从 A 列中修剪空白单元格并删除 A 列中带有空白单元格的整行的宏 - Macro that clean trim the blank cells from only column A and delete entire row with blank cell in column A 过滤指定的列并检查是否有空白单元格,然后删除整行 - Filter on a specified column and check for blank if there are blank cells then deletes the entire row 如果任何列中的所有行都是空白,则删除整列+跳过第一行 - If all rows in any column are blank then delete entire column + skip first row Excel 宏/VBA - 如果一列中的单元格为空,如何删除整行? - Excel Macro / VBA - How do I delete an entire row if a cell in one column is blank? 删除列中的空白单元格 - Delete blank cells in a column excel宏:删除具有1个或多个空白单元格的行并向上移动数据 - excel macro: Delete row with 1 or more blank cells and shift data up Excel VBA:如果列A中的单元格为空(长数据集),则删除整行 - Excel VBA: Delete entire row if cell in column A is blank (Long Dataset) 如果一个字段为空/空白,则尝试使用 VBA 删除整行 - Trying to use VBA to delete an entire row if one field in null/blank 如果整个范围为空白,则删除整行吗? - Delete entire row if entire range is blank?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM