Delete all data rows from an Excel table (apart from the first)

后端 未结 9 2112
执笔经年
执笔经年 2020-12-01 09:16

Just recently I\'ve been trying to delete all data rows in a table, apart from the first (which needs to just be cleared)

Some of the tables being actioned could alr

相关标签:
9条回答
  • 2020-12-01 10:13

    This is how I clear the data:

    Sub Macro3()
        With Sheet1.ListObjects("Table1")
            If Not .DataBodyRange Is Nothing Then
                .DataBodyRange.Delete
            End If
        End With
    End Sub
    
    0 讨论(0)
  • 2020-12-01 10:21

    I'm simply using this:

    On Error Resume Next
    Worksheets("Sheet1").ListObjects("Table1").DataBodyRange.Rows.Delete
    

    The first line stays in all cases (it is cleared, of course).

    0 讨论(0)
  • 2020-12-01 10:22

    The codes above wouldn't work in Excel 2010 My code bellow allows you to go through number of sheets you would like then select tables and delete rows

    Sub DeleteTableRows()
    Dim table As ListObject
    Dim SelectedCell As Range
    Dim TableName As String
    Dim ActiveTable As ListObject
    
    'select ammount of sheets want to this to run
    For i = 1 To 3
        Sheets(i).Select
        Range("A1").Select
        Set SelectedCell = ActiveCell
        Selection.AutoFilter
    
        'Determine if ActiveCell is inside a Table
        On Error GoTo NoTableSelected
        TableName = SelectedCell.ListObject.Name
        Set ActiveTable = ActiveSheet.ListObjects(TableName)
        On Error GoTo 0
    
        'Clear first Row
        ActiveTable.DataBodyRange.Rows(1).ClearContents
        'Delete all the other rows `IF `they exist
        On Error Resume Next
        ActiveTable.DataBodyRange.Offset(1, 0).Resize(ActiveTable.DataBodyRange.Rows.Count - 1, _
        ActiveTable.DataBodyRange.Columns.Count).Rows.Delete
        Selection.AutoFilter
        On Error GoTo 0
    Next i
    Exit Sub
    'Error Handling
    NoTableSelected:
      MsgBox "There is no Table currently selected!", vbCritical
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题