循环遍历多个特定工作表中的listobjects

时间:2016-05-26 09:11:06

标签: excel-vba foreach listobject vba excel

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

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

1 个答案:

答案 0 :(得分:1)

您正在遍历工作表而不是列表对象,因此您无法以这种方式在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