如何在不破坏表格的情况下选择清除表格内容?

问题描述 投票:13回答:5

我在excel 2010中有一个vba函数,我是在这里使用人们的帮助构建的。此函数复制表/表单的内容,对其进行排序,并将它们发送到适当的表。

现在运行此函数后,我希望清除原始表。我可以使用以下代码实现此目的,假设ACell已被定义为表中的第一个单元格。 ACell.ListObject.Range.ClearContents工作正常,唯一的问题是它删除表以及数据值。

有没有办法解决?每次输入一些数据时,我宁愿不必设置表格。

excel-vba excel-2010 vba excel
5个回答
30
投票

怎么样:

ACell.ListObject.DataBodyRange.Rows.Delete

这将保留您的表结构和标题,但清除所有数据和行。

编辑:我将只修改your previous post的一部分答案,因为它主要是你想要的。这只留下一行:

With loSource
   .Range.AutoFilter
   .DataBodyRange.Offset(1).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.Delete
   .DataBodyRange.Rows(1).Specialcells(xlCellTypeConstants).ClearContents
End With

如果你想保留所有行的公式和诸如此类的东西,那么就这样做:

With loSource
   .Range.AutoFilter
   .DataBodyRange.Specialcells(xlCellTypeConstants).ClearContents
End With

哪个接近@Readify所建议的,除了它不会清除公式。


7
投票

尝试清除数据(而不是整个表格,包括标题):

ACell.ListObject.DataBodyRange.ClearContents

3
投票

我重写了Doug Glancy的解决方案以避免删除行,这可能会导致公式中出现#Ref问题。

Sub ListReset(lst As ListObject)
'clears a listObject while leaving row 1 empty, with formulae
    With lst
        If .ShowAutoFilter Then .AutoFilter.ShowAllData
        On Error Resume Next
        With .DataBodyRange
            .Offset(1).Rows.Clear
            .Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
        End With
        On Error GoTo 0
        .Resize .Range.Rows("1:2")
    End With
End Sub

1
投票

我使用此代码删除我的数据,但将公式保留在顶行。它还会删除除顶行之外的所有行,并将页面向上滚动到顶部。

Sub CleanTheTable()
    Application.ScreenUpdating = False
    Sheets("Data").Select
    ActiveSheet.ListObjects("TestTable").HeaderRowRange.Select
    'Remove the filters if one exists.
    If ActiveSheet.FilterMode Then
    Selection.AutoFilter
    End If
    'Clear all lines but the first one in the table leaving formulas for the next go round.
    With Worksheets("Data").ListObjects("TestTable")
    .Range.AutoFilter
    On Error Resume Next
    .DataBodyRange.Offset(1).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.Delete
    .DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
    ActiveWindow.SmallScroll Down:=-10000

    End With
Application.ScreenUpdating = True
End Sub

1
投票

有一种情况是大多数这些解决方案都没有解决。我修改了Patrick Honorez的解决方案来处理它。我觉得我必须分享这个,因为当原来的功能偶尔清除我预期的更多数据时,我正在把头发拉出来。

当表只有一列并且.SpecialCells(xlCellTypeConstants).ClearContents尝试清除顶行的内容时,就会发生这种情况。在这种情况下,只选择一个单元格(表格的顶行只有一列),而SpecialCells命令适用于整个工作表而不是选定的范围。发生在我身上的是桌子外面的其他单元格也被清除了。

我做了一些挖掘,并从Mathieu Guindon那里找到了这个建议:Range SpecialCells ClearContents clears whole sheet

范围({任何单个单元格})。特殊细胞({whatever})似乎在整个工作表中起作用。

范围({多于一个单元})。SpecialCells({whatever})似乎在指定的单元格之外工作。

如果列表/表只有一列(在第1行),则此修订将检查单元格是否具有公式,如果没有,则仅清除该单元格的内容。

Public Sub ClearList(lst As ListObject)
'Clears a listObject while leaving 1 empty row + formula
' https://stackoverflow.com/a/53856079/1898524
'
'With special help from this post to handle a single column table.
'   Range({any single cell}).SpecialCells({whatever}) seems to work off the entire sheet.
'   Range({more than one cell}).SpecialCells({whatever}) seems to work off the specified cells.
' https://stackoverflow.com/questions/40537537/range-specialcells-clearcontents-clears-whole-sheet-instead

    On Error Resume Next

    With lst
        '.Range.Worksheet.Activate ' Enable this if you are debugging 

        If .ShowAutoFilter Then .AutoFilter.ShowAllData
        If .DataBodyRange.Rows.Count = 1 Then Exit Sub ' Table is already clear
        .DataBodyRange.Offset(1).Rows.Clear

        If .DataBodyRange.Columns.Count > 1 Then ' Check to see if SpecialCells is going to evaluate just one cell.
            .DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
        ElseIf Not .Range.HasFormula Then
            ' Only one cell in range and it does not contain a formula.
            .DataBodyRange.Rows(1).ClearContents
        End If

        .Resize .Range.Rows("1:2")

        .HeaderRowRange.Offset(1).Select

        ' Reset used range on the sheet
        Dim X
        X = .Range.Worksheet.UsedRange.Rows.Count 'see J-Walkenbach tip 73

    End With

End Sub

我包含的最后一步是John Walkenbach的一个提示,有时被称为J-Walkenbach tip 73 Automatically Resetting The Last Cell

© www.soinside.com 2019 - 2024. All rights reserved.