• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Selecting Table on Sheet

Tripp

Member
Hello,

I am tying to clear the contents of a table so that when I paste in new values the data will still have the same table name. What I have so far requires me to have clicked on the table so that 'Activecell' Works. I would like it so that the vba selects the table without the user having to.

Code:
Sub ClearTable()

Application.ScreenUpdating = False


'Clears the table contents

If Not ActiveCell.ListObject Is Nothing Then
ActiveCell.ListObject.DataBodyRange.Rows.ClearContents
End If

'Shrinks the Table  to one blank row

If Not ActiveCell.ListObject Is Nothing Then
    ActiveCell.ListObject.DataBodyRange.Delete
End If

Application.ScreenUpdating = True

End Sub

Thanks
Tripp
 
Hi ,

See if this works :
Code:
Sub ClearTable()
    Dim lo As ListObject
    Application.ScreenUpdating = False

    Set lo = ActiveSheet.ListObjects("Table1")  ' Change the name of the table to suit

    If Not lo Is Nothing Then
      lo.DataBodyRange.Delete
    End If
   
    Application.ScreenUpdating = True
End Sub
Narayan
 
Hi Narayan,

I've pasted your code in but it says the sub/ function is not defined and highlights the macro title in the code.. Not sure how to amend.
 
Hi ,

Where have you pasted this code ? In a Worksheet section , a code module or in the ThisWorkbook section ?

Paste it in the worksheet section corresponding to the worksheet where you have your table. If you paste it in a code module , ensure that the worksheet where you have your table is activated before you run the macro.

Narayan
 
I have pasted the code into the Worksheet with the table and get the same response. I am now getting this error when I paste the original code back to the same place...?
 
Back
Top