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:

[TABLE="width: 500"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Employee[/TD]
[TD]Region[/TD]
[TD]Sales[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]John[/TD]
[TD]Spain[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Mike[/TD]
[TD]UK[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Peter[/TD]
[TD]Germany[/TD]
[TD]30[/TD]
[/TR]
</tbody>[/TABLE]

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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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