Search code examples
excelvbaexcel-2013

Clearing Data from Multiple Tables in VBA


I have a series of 20 tables on a sheet called "Builder" all with there own individual names. What i want to do is create a variable that isolates only the 19 tables that i tell it to so that i create a clear content procedure on them. The 1 remaining table needs to untouched as this table is a overview of the other 19. I have found the below to delete the rows of 1 table but i need to expand it to running the process on multiple.

Sub Macro3()
    With Sheets(Builder").ListObjects("P6WC_00002")
        If Not .DataBodyRange Is Nothing Then
             .DataBodyRange.Delete
        End If
    End With
End Sub

A few other examples of tables names are: D86-03116, D87-03215, F08-00025

Thanks in advance


Solution

  • Use a For Each loop, and an Exclusion check:

    Dim TableToCheck AS ListObject
    
    For Each TableToCheck In ThisWorkbook.Worksheets("Builder").ListObjects
        If TableToCheck.Name <> "P6WC_00000" Then 'Name of Table you do NOT want to update
            If Not (TableToCheck.DataBodyRange Is Nothing) Then TableToCheck.DataBodyRange.Delete
        End If
    Next TableToCheck