Check if Table Databodyrange has value

I would like help in this section of my code where I want it to show a msgbox if the table already has data, but I am not able to set it. With my current code it shows the msg regardless of whether or not there is data in the table.

If Not ActiveSheet.ListObjects(1).DataBodyRange Is Nothing Then
    MsgBox "Table has data", vbInformation
    Exit Sub
End If

>Solution :

Use WorksheetFunction.CountA to count the non-empty cells in the table body:

With ActiveSheet.ListObjects(1)
   If Not .DataBodyRange Is Nothing Then
       If WorksheetFunction.CountA(.DataBodyRange) > 0 Then
           MsgBox "Table has data", vbInformation
           Exit Sub
       End If
   End If
End With

Leave a Reply