I have the following code that generates an error.
I want to loop through all the tables on the active worksheet and delete the data except for the data on two specified in the code.
Sub Clear_Tables()
Dim tbl As ListObject
For Each tbl In ActiveSheet.ListObjects
If tbl <> "Table_Extracted_Data_Summary" Or tbl <> "Manual_Entries" Then
tbl.DataBodyRange.Rows.Delete
Else
End If
Next tbl
End Sub
The error code is:
Run-time error '91':
Object variable or With block variable not set
I have got the following code working but for some reason if deletes the contents of the 2 tables i want to leave
Sub Clear_Tables()
'PURPOSE: Loop through and apply a change to all Tables in the Active Excel
Sheet
Dim TableToCheck As ListObject
For Each TableToCheck In ActiveSheet.ListObjects
If TableToCheck.Name = "Table_Extracted_Data_Summary" Or
TableToCheck.Name = "Manual_Entries" Then 'Name of Table you do NOT want to
update
If Not (TableToCheck.DataBodyRange Is Nothing) Then
TableToCheck.DataBodyRange.ClearContents
End If
Next TableToCheck
End Sub
Amend your second code to the following. You want tables whose name is NOT A and is NOT B.
Sub Clear_Tables()
'PURPOSE: Loop through and apply a change to all Tables in the Active Excel
Sheet
Dim TableToCheck As ListObject
For Each TableToCheck In ActiveSheet.ListObjects
If TableToCheck.Name <> "Table_Extracted_Data_Summary" And _
TableToCheck.Name <> "Manual_Entries" Then 'Name of Table you do NOT want to update
If Not (TableToCheck.DataBodyRange Is Nothing) Then
TableToCheck.DataBodyRange.ClearContents
End If
End If
Next TableToCheck
End Sub
Or revert from ClearContents
to Rows.Delete
if appropriate.