VBA Determine if Table body is empty

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,345
Office Version
  1. 365
Platform
  1. Windows
Is there an easy way to determine if the "DataBodyRange" of a table is empty? I want to use some if/then statements to create some error messages if there is no data in a specific table. Thanks in advance for the help!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Maybe "SpecialCells"?

Something like this:

Code:
Public Sub Test()

Dim oBlank As Range
Dim oTestRange As Range
Dim oSheet As Worksheet

Set oSheet = ActiveSheet 'Adjust sheet to suit
Set oTestRange = oSheet.Range("A1:F30") 'Adjust range to suit

Set oBlank = oTestRange.SpecialCells(xlCellTypeBlanks)

If oBlank.Cells.Count = oTestRange.Cells.Count Then
    MsgBox "Specified range is empty"
Else
    MsgBox "Specified range is NOT empty"
End If

End Sub
 
Upvote 0
Maybe "SpecialCells"?

Something like this:

Code:
Public Sub Test()

Dim oBlank As Range
Dim oTestRange As Range
Dim oSheet As Worksheet

Set oSheet = ActiveSheet 'Adjust sheet to suit
Set oTestRange = oSheet.Range("A1:F30") 'Adjust range to suit

Set oBlank = oTestRange.SpecialCells(xlCellTypeBlanks)

If oBlank.Cells.Count = oTestRange.Cells.Count Then
    MsgBox "Specified range is empty"
Else
    MsgBox "Specified range is NOT empty"
End If

End Sub
This worked for me:

Public Function CheckTbl(tbl As ListObject) As Boolean
With tbl
Dim bempty As Boolean
'Check if table is empty
If .DataBodyRange Is Nothing Then
bempty = True
Else
'Table must not be empty
bempty = False
End If
End With
CheckTbl = bempty
End Function
 
Upvote 0
This worked for me:

Public Function CheckTbl(tbl As ListObject) As Boolean
With tbl
Dim bempty As Boolean
'Check if table is empty
If .DataBodyRange Is Nothing Then
bempty = True
Else
'Table must not be empty
bempty = False
End If
End With
CheckTbl = bempty
End Function
Hi, this does not work when the .DatabodyRange is empty and return error 91.
 
Upvote 0

Forum statistics

Threads
1,222,610
Messages
6,167,050
Members
452,093
Latest member
JamesFromAustin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top