简体   繁体   English

仅当该行中有多个单元格为空时,才如何从Excel中的表中删除整行? (VBA)

[英]How to delete an entire row from a table in excel only if multiple cells are empty in that row? (VBA)

I Have 10 columns in an Excel table, and I want to delete the rows where the first 7 cell is empty. 我在Excel表中有10列,并且我想删除前7个单元格为空的行。 I've tried to do it this way: 我已经尝试过这种方式:

Sheet1.Range("Table4[variable1, variable2, variable3, variable4, variable5, variable6, variable7]").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

but It doesn't work. 但这不起作用。 Am I have to use nested for loop for rows and columns? 我必须对行和列使用嵌套的for循环吗?

Broadly speaking yes. 大致来说是的。 Loop down the rows you want to check, 循环查看要检查的行,

  For rowcounter = 1 to 10  'whatever rows you want

use the test 使用测试

  If Application.WorksheetFunction.CountA("A" & rowcounter & ":G" & rowcounter) = 0 Then

(I assume first 7 columns meant A to G), and then (我假设前7列表示A到G),然后

 Rows(rowcounter).Delete

You don't need multiple loops. 您不需要多个循环。 A single loop with the use of the IsEmpty() function should work: 使用IsEmpty()函数的单个循环应该可以工作:

Option Explicit

Sub Test()
    Dim i As Long

    For i = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row To 1 Step -1
        If IsEmpty(Sheet1.Cells(i,1)) And IsEmpty(Sheet1.Cells(i,2)) And IsEmpty(Sheet1.Cells(1,3)) And _
           IsEmpty(Sheet1.Cells(i,4)) And IsEmpty(Sheet1.Cells(i,5)) And _
           IsEmpty(Sheet1.Cells(i,6)) And IsEmpty(Sheet1.Cells(i,7)) Then
               Sheet1.Rows(i).Delete
        End If
    Next i
End Sub

You can loop trough each row directly, and check if the first 7 cells of that row in your table are empty. 您可以直接遍历每行,并检查表中该行的前7个单元格是否为空。 If true, delete them. 如果为true,请删除它们。

Dim MyTable As ListObject
Dim i As Long

Set MyTable = ActiveSheet.ListObjects("Table4")


With MyTable.DataBodyRange
    For i = .Rows.Count To 1 Step -1
        If Application.WorksheetFunction.CountBlank(.Range(Cells(i, 1), Cells(i, 7))) = 7 Then .Rows(i).Delete
    Next i
End With

The good point about this way is that if your table changes address, it still will work. 这种方法的好处是,如果您的表更改了地址,它仍然可以工作。 You would only need to update if you want to check a different name of cells (seven rght now) or if the condition (7 first cells empty) changes. 仅当要检查其他单元格名称(现在为七个)或条件(第7个单元格为空)改变时,才需要更新。

I guess that this simple snippet, full of unnecessary procedures, can help you: 我想这个简单的代码段包含了不必要的步骤,可以帮助您:

Sub NotTested()

    ' Choose below the rows range
    first_row = 2
    last_row = 4242

    For r = last_row To first_row Step -1

        ' Checking below each column (from row r) value
        a_value = ThisWorkbook.Sheets("Sheet1").Cells(r, 1).Value2
        b_value = ThisWorkbook.Sheets("Sheet1").Cells(r, 2).Value2
        c_value = ThisWorkbook.Sheets("Sheet1").Cells(r, 3).Value2
        d_value = ThisWorkbook.Sheets("Sheet1").Cells(r, 4).Value2
        e_value = ThisWorkbook.Sheets("Sheet1").Cells(r, 5).Value2
        f_value = ThisWorkbook.Sheets("Sheet1").Cells(r, 6).Value2
        g_value = ThisWorkbook.Sheets("Sheet1").Cells(r, 7).Value2

        ' Comparing if the columns are actually empty
        If a_value = "" And b_value = "" And c_value = "" And d_value = "" And e_value = "" And f_value = "" And g_value = "" Then            
            ThisWorkbook.Sheets("Sheet1").Cells(r, 1).EntireRow.Delete             
        End If

    Next r

End Sub

Here's a simple solution that actually counts the number of rows in a table then deletes if the first 7 columns are blank. 这是一个简单的解决方案,它实际上计算表中的行数,如果前7列为空白,则删除该行。

Sub deleteEmptyRows()
Set tbl = ActiveSheet.ListObjects("Table4")
For I = 1 To tbl.Range.Rows.Count
    If WorksheetFunction.CountA(Range("A" & I & ":" & "G" & I)) = 0 Then
        Sheets("Sheet1").Rows(I).EntireRow.Delete
    End If
Next I
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM