Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check If Any ListObjects Exist in Worksheet

Tags:

excel

vba

If I am looping through my sheets and want to test if any ListObjects exist in the given sheet, how do I do this? I tried this:

Dim daysoftheweek As Worksheet
For Each daysoftheweek In Sheets
If Not daysoftheweek.ListObjects Is Nothing Then
'Do Something Here - Only If The Sheet Contains A List Object
End If
Next

But the IF conditional block still ran even when the sheet did not contain a ListObject.

like image 258
Summer Developer Avatar asked Jul 15 '15 02:07

Summer Developer


1 Answers

Worksheet.ListObjects is a collection so it isn't Nothing. Like Paul Bica commented, you want to count how many objects are in the collection. Try this:

Dim daysoftheweek As Worksheet

For Each daysoftheweek In Sheets
    If daysoftheweek.ListObjects.Count > 0 Then
        'Do Something Here - Only If The Sheet Contains A List Object
    End If
Next
like image 84
PermaNoob Avatar answered Sep 30 '22 06:09

PermaNoob