简体   繁体   English

使用VBA删除Excel表中的空行

[英]Delete empty rows in an Excel table using VBA

I am trying to run a code to delete rows when the whole row has no data. 我正在尝试运行代码以在整行没有数据时删除行。 I am currently using the code below, but it is deleting rows when even one cell is empty. 我目前正在使用下面的代码,但即使有一个单元格为空,它也会删除行。 I think I need to use intersect function but not sure how to as yet. 我想我需要使用相交函数,但还不确定如何使用。

    Dim Rng As Range
    Dim MainSheet As Worksheet
    Set MainSheet = Sheet9
    MainSheet.Select

    On Error Resume Next
    Set Rng = MainSheet.Range("table3").SpecialCells(xlCellTypeBlanks)

    On Error Resume Next
    If Not Rng Is Nothing Then
       Rng.Delete Shift:=xlUp
    End If

It really all depends on what you mean by empty. 实际上,这完全取决于您的意思是什么。 I notice you're using xlCellTypeBlanks , so if you wanted to continue using that range property, then you could just compare the row's cell count with your SpecialCells cell count - if they match, then you have an 'empty' row. 我注意到您正在使用xlCellTypeBlanks ,因此,如果您要继续使用该range属性,则可以将行的单元格计数与SpecialCells单元格计数进行比较-如果它们匹配,则您有一个“空”行。

There are many others ways of doing this, but I'm not aware of one which doesn't need to iterate each row in your table. 还有许多其他方法可以执行此操作,但是我不知道一种方法不需要迭代表中的每一行。 Assuming you want to go the SpecialCells route, your iteration could be something like this: 假设您想走SpecialCells路线,您的迭代可能是这样的:

Dim lo As ListObject
Dim lRow As ListRow
Dim rng As Range
Dim delRows As Collection

Set lo = Sheet1.ListObjects("Table1") 'change to your table name
On Error Resume Next
For Each lRow In lo.ListRows
    Set rng = Nothing
    Set rng = lRow.Range.SpecialCells(xlCellTypeBlanks)
    If Not rng Is Nothing Then
        If rng.Count = lRow.Range.Cells.Count Then
            If delRows Is Nothing Then
                Set delRows = New Collection
                delRows.Add lRow
            Else
                delRows.Add lRow, Before:=1
            End If
        End If
    End If
Next
On Error GoTo 0

If Not delRows Is Nothing Then
    For Each lRow In delRows
        lRow.Delete
    Next
End If

This is an alternate solution but should be considered in bad form since it effectively is altering the table count when it deletes a row. 这是一个替代解决方案,但应以错误的形式考虑,因为它在删除行时会有效地更改表计数。 That's what amounts to bad programming practice. 那就是不良的编程习惯。

Also if there are embedded blanks, it won't catch them. 另外,如果有嵌入的毛坯,也不会抓住它们。 FYI I debugged using Sheet 1 instead of Sheet 9. 仅供参考,我使用工作表1而不是工作表9进行了调试。

On the plus side it is more concise (for whatever that might be worth). 从好的方面来说,它更加简洁(无论可能有价值的东西)。

There are also VBA quirks that I don't fully appreciate. 还有一些我不完全欣赏的VBA怪癖。 For example if you replace 'rng' in the find command with it's expression you'll get an error which I'd consider counter-intuitive unless someone knows why this should make sense. 例如,如果在find命令中用表达式替换“ rng”,将得到一个错误,除非有人知道为什么这样做,否则我会认为这是违反直觉的。

Sub DeleteEmptyTableRows()
    Dim MainSheet As Worksheet
    Dim tabRng As Range, rng As Range, found As Range
    Dim row As Integer

    Set MainSheet = Sheet1
    MainSheet.Select

    Set tabRng = MainSheet.Range("table3")

    row = 1
    Do While row < tabRng.Rows.Count + 1:
      Set rng = tabRng.Rows(row)
      Set found = rng.Find("*", rng.Cells(, 1), SearchDirection:=xlPrevious)
      If found Is Nothing Then
         tabRng.Rows(row).EntireRow.Delete Shift:=xlUp
      Else
         row = row + 1
      End If
    Loop
End Sub

If you find any bugs, please let me know. 如果您发现任何错误,请告诉我。 Aside from not deleting entries that had blanks in them, it worked for whatever test data I used. 除了不删除其中包含空格的条目外,它还适用于我使用的任何测试数据。

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

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