最近我一直在尝试删除表格中除第一行之外的所有数据行(第一行需要清空)。
有些被操作的表格可能已经没有行,所以当在没有行(只有标题和/或页脚)的表格上使用 .DataBodyRange.Rows.Count
时会导致错误。
我四处寻找解决方案,但并未找到完整的答案,因此我希望我的答案能够对将来的其他人有所帮助。
最近我一直在尝试删除表格中除第一行之外的所有数据行(第一行需要清空)。
有些被操作的表格可能已经没有行,所以当在没有行(只有标题和/或页脚)的表格上使用 .DataBodyRange.Rows.Count
时会导致错误。
我四处寻找解决方案,但并未找到完整的答案,因此我希望我的答案能够对将来的其他人有所帮助。
这是我清除数据的方法:
Sub Macro3()
With Sheet1.ListObjects("Table1")
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.Delete
End If
End With
End Sub
With Sheet1.ListObjects("Table1")
行更改为 With Range("Table1").ListObject
。 - Ben您的代码可以缩小到
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我有三个例程,它们都运行良好,只需要在表格中选择一个单元格并运行其中的一个子程序。
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
我希望保留公式的位置,但上述代码没有做到。
以下是我的做法,注意这会在表格中留下一行空白。
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
On Error Resume Next
Worksheets("Sheet1").ListObjects("Table1").DataBodyRange.Rows.Delete
第一行在所有情况下都保留(当然会被清除)。
这个方案对你是否可行?我已经在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
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
我建议先清空内容,然后调整表格大小:
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
这个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
Err.clesr
应该是 Err.Clear
。 - engineersmnkyIf
,因为它们都检查不同的东西。但是,我正在查看另一个答案,可以完全避免使用 If
语句。谢谢。 - David GardWith [TableName].ListObject
If Not .DataBodyRange Is Nothing Then: .DataBodyRange.Delete
End With
不需要表格引用等。