从Excel表格中删除所有数据行(除第一行外)

26

最近我一直在尝试删除表格中除第一行之外的所有数据行(第一行需要清空)。

有些被操作的表格可能已经没有行,所以当在没有行(只有标题和/或页脚)的表格上使用 .DataBodyRange.Rows.Count 时会导致错误。

我四处寻找解决方案,但并未找到完整的答案,因此我希望我的答案能够对将来的其他人有所帮助。


如果你不想使用异常来处理代码流程,我猜想你可能会因为在调用的某个点上遇到了空引用异常(如果Table、DataBodyRange或Rows为空,你将会出现错误)。你可以在调用之前进行错误检查来解决这个问题。 - Seth Moore
11个回答

39

这是我清除数据的方法:

Sub Macro3()
    With Sheet1.ListObjects("Table1")
        If Not .DataBodyRange Is Nothing Then
            .DataBodyRange.Delete
        End If
    End With
End Sub

6
大多数这里的答案假设你的表格在当前工作表上或者你知道工作表的名称。如果要清除一个命名表格,而不需要引用工作表,请将您的 With Sheet1.ListObjects("Table1") 行更改为 With Range("Table1").ListObject - Ben
也许使用Application.Range会更全面,但只要我理解正确的话选择合适的方法就可以了。 - JeopardyTempest

24

您的代码可以缩小到

Sub DeleteTableRows(ByRef Table As ListObject)
    On Error Resume Next
    '~~> Clear Header Row `IF` it exists
    Table.DataBodyRange.Rows(1).ClearContents
    '~~> Delete all the other rows `IF `they exist
    Table.DataBodyRange.Offset(1, 0).Resize(Table.DataBodyRange.Rows.Count - 1, _
    Table.DataBodyRange.Columns.Count).Rows.Delete
    On Error GoTo 0
End Sub

编辑:

顺带一提,如果我需要告知用户第一行或其他行是否已删除,我会添加适当的错误处理。


是的,我想我在这种情况下确实不需要专门重置错误。但是,End Sub 会重置错误吗?还是我仍然需要手动重置? - David Gard
不需要让用户知道行是否已被删除,这只是为了确保清除任何旧数据。 - David Gard

11

我有三个例程,它们都运行良好,只需要在表格中选择一个单元格并运行其中的一个子程序。

Sub ClearTable()
If Not ActiveCell.ListObject Is Nothing Then
    ActiveCell.ListObject.DataBodyRange.Rows.ClearContents
End If
End Sub

缩小表格范围以除去数据主体范围,但保留标题和第一行数据

Sub ShrinkTable()
If Not ActiveCell.ListObject Is Nothing Then
    ActiveCell.ListObject.DataBodyRange.Delete
End If
End Sub

删除表格以完全从工作表中删除该表格

Sub DeleteTable()
If Not ActiveCell.ListObject Is Nothing Then
    ActiveCell.ListObject.Delete
End If
End Sub

做得好,区分清楚、缩小和删除。 - Tulio Casagrande

4

我希望保留公式的位置,但上述代码没有做到。

以下是我的做法,注意这会在表格中留下一行空白。

Sub DeleteTableRows(ByRef Table As ListObject, KeepFormulas as boolean)

On Error Resume Next

if not KeepFormulas then
    Table.DataBodyRange.clearcontents
end if

Table.DataBodyRange.Rows.Delete

On Error GoTo 0

End Sub

(PS不要问我为什么!)

3
我只是简单地使用这个:
On Error Resume Next
Worksheets("Sheet1").ListObjects("Table1").DataBodyRange.Rows.Delete

第一行在所有情况下都保留(当然会被清除)。


2

这个方案对你是否可行?我已经在Excel 2010中进行了测试,它可以正常工作。 这是使用名为“Table1”的表格进行操作的,该表格使用A到G列。

Sub Clear_Table()
    Range("Table1").Select
    Application.DisplayAlerts = False
    Selection.Delete
    Application.DisplayAlerts = True
    Range("A1:G1").Select
    Selection.ClearContents
End Sub

0
操作表格的另一个方面是,如果表格有一个正在隐藏行的筛选器,那么在修改表格之前,显示所有数据可能会得到更好的结果。我使用 ActiveCell.ListObject.ShowAllData
Sub ClearTable()
   If Not ActiveCell.ListObject Is Nothing Then
      On Error Resume Next
      ActiveCell.ListObject.ShowAllData
      ActiveCell.ListObject.DataBodyRange.Rows.ClearContents
   End If
End Sub

0

我建议先清空内容,然后调整表格大小:

Sub DeleteTableRows(ByRef Table As ListObject)

     Dim R               As Range

On Error Resume Next

    Table.DataBodyRange.ClearContents
    Set R = Table.Range.Rows(1).Resize(2)
    Table.Resize R

On Error GoTo 0

End Sub

0

这个VBA Sub将删除所有数据行(除了第一行,它只会清除)-

Sub DeleteTableRows(ByRef Table as ListObject)

        '** Work out the current number of rows in the table
        On Error Resume Next                    ' If there are no rows, then counting them will cause an error
        Dim Rows As Integer
        Rows = Table.DataBodyRange.Rows.Count   ' Cound the number of rows in the table
        If Err.Number <> 0 Then                 ' Check to see if there has been an error
            Rows = 0                            ' Set rows to 0, as the table is empty
            Err.Clear                           ' Clear the error
        End If
        On Error GoTo 0                         ' Reset the error handling

        '** Empty the table *'
        With Table
            If Rows > 0 Then ' Clear the first row
                .DataBodyRange.Rows(1).ClearContents
            End If
            If Rows > 1 Then ' Delete all the other rows
                .DataBodyRange.Offset(1, 0).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.Delete
            End If
        End With

End Sub

1
如果你问这个问题,我们会给你类似的答案 :) 但是你不需要两个 IF 条件。你可以将它们合并。 - Siddharth Rout
2
我同意 @SiddharthRout 的观点。此外,您有一个拼写错误 Err.clesr 应该是 Err.Clear - engineersmnky
@engineersmnky - 谢谢,我刚在VBA中发现了这个问题! - David Gard
@SiddharthRout - 在这种情况下,我确实需要2个 If,因为它们都检查不同的东西。但是,我正在查看另一个答案,可以完全避免使用 If 语句。谢谢。 - David Gard

0
如果您已经提前知道表名,这是我会使用的一种简短方法。
With [TableName].ListObject
    If Not .DataBodyRange Is Nothing Then: .DataBodyRange.Delete
End With

不需要表格引用等。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接