循环遍历多个特定工作表中的列表对象

Looping through listobjects in multiple specific sheets

我一直在尝试遍历多个特定工作表中的列表对象,以检查列表对象是否包含数据,但它似乎不起作用:

Dim SheetsArray_Reports As Sheets
Dim ListObject_Reports As ListObject

Set SheetsArray_Reports = ThisWorkbook.Worksheets(Array(All_CleaningRangeReport.Name, SD_CleaningRangeReport.Name, SC_CleaningRangeReport.Name, Excl_CleaningRangeReport.Name))

For Each ListObject_Reports In SheetsArray_Reports

        If Not ListObject_Reports.DataBodyRange Is Nothing Then

            ListObject_Reports.DataBodyRange.Delete

        End If

Next ListObject_Reports

您正在遍历工作表而不是列表对象,因此您不能以这种方式在 for 循环中引用它们。尝试使用

Dim SheetsArray_Reports As Sheets
Dim ws As Worksheet
Dim ListObject_Reports As ListObject

Set SheetsArray_Reports = ThisWorkbook.Worksheets(Array(All_CleaningRangeReport.Name, SD_CleaningRangeReport.Name, SC_CleaningRangeReport.Name, Excl_CleaningRangeReport.Name))

For Each ws In SheetsArray_Reports
    ' Use if only 1 listobject per worksheet if more you will have to loop through them with something like:
        'For Each ListObject_Reports In ws.ListObjects
        '
        'Next ListObject_Reports
    Set ListObject_Reports = ws.ListObjects(1)

    If ListObject_Reports.DataBodyRange.Rows.Count > 1 Then
        ListObject_Reports.DataBodyRange.Delete
    End If
Next ListObject_Reports