Excel Table - vba code to delete data and clear content

Dancarro

Board Regular
Joined
Feb 23, 2013
Messages
65
Hi,

I am struggling with vba code which is supposed to do the following:

IDEmployeeRegionSales
1JohnSpain10
2MikeUK20
3PeterGermany30

<tbody>
</tbody>

In the TABLE (not range) I need that row 3 until last row is deleted and that the second row for columns ID:Region that this row is cleared content; column Sales has formula based on the three first columns

I have the following code which is not working:

Sub Test()




Dim loSource As Excel.ListObject
Dim laSource As Excel.ListObject

Set loSource = Sheets("Sales").ListObjects("Table1")
loSource.ListObjects(2).DataBodyRange.Delete Shift:=xlShiftUp


Set laSource = Sheets("Sales").ListObjects("Table1")
laSource.ListColumns.Range("Table1[[ID]:[Region]]").Select
Selection.ClearContents

End Sub

Thank you
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Is this what you want?
It clears the whole table, leaving only the headers and first row formulas

Test on a copy of your worksheet

Code:
Sub ClearTable()
    Dim T As ListObject:    Set T = ActiveSheet.ListObjects(1)
    With T.DataBodyRange
        If .Rows.Count > 1 Then .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete
        On Error Resume Next
        .Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
    End With
End Sub
 
Last edited:
Upvote 0
In the TABLE (not range) I need that row 3 until last row is deleted and that the second row for columns ID:Region that this row is cleared content; column Sales has formula based on the three first columns
Try this:
Code:
Public Sub Delete_Rows_and_Clear_Cells_In_Table()
    
    Dim table As ListObject
    
    Set table = Sheets("Sales").ListObjects("Table1")
    
    With table
        'Delete 3rd data row to last data row
        .DataBodyRange.Rows("3:" & .ListRows.Count).Delete
        
        'Clear first 3 cells on 2nd data row
        .DataBodyRange(2, 1).Resize(1, 3).ClearContents
    End With
    
End Sub
 
Upvote 0
Is this what you want?
It clears the whole table, leaving only the headers and first row formulas

Test on a copy of your worksheet

Code:
Sub ClearTable()
    Dim T As ListObject:    Set T = ActiveSheet.ListObjects(1)
    With T.DataBodyRange
        If .Rows.Count > 1 Then .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete
        On Error Resume Next
        .Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
    End With
End Sub
Hi Yongle,

How can I make this code work on three tables on different sheets?

Thank you
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,716
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top