Excel Power Query - check table to see if its blank

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,347
Office Version
  1. 365
Platform
  1. Windows
I have VBA code with this line in it.

'Move data from PQ Cost Source
Sheets("PQ Cost Source").ListObjects("PQ_Cost_Source").DataBodyRange.Copy Sheets("Tempory Table CS").Range("A2")'Move data from PQ Cost Source
Sheets("PQ Cost Source").ListObjects("PQ_Cost_Source").DataBodyRange.Copy Sheets("Tempory Table CS").Range("A2")


Is there a way to check the table to see if there is any data before it copies?

Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Two methods:

VBA Code:
If Sheets("PQ Cost Source").ListObjects("PQ_Cost_Source").ListRows.Count > 0 Then
    Msgbox "There is data in the table"
End If

VBA Code:
If Not Sheets("PQ Cost Source").ListObjects("PQ_Cost_Source").DataBodyRange is Nothing Then
    Msgbox "There is data in the table"
End If

There is a trick here. Say there is no data in the table, but the first row is still a row which is ready for data entry, because you deleted all rows by actuall deleting the rows. However, let's say you had one row with data, but instead deleting the row, you cleared content of the cells. In this case, the first two methods above will not know that the table is empty.

In this case, the following could be more reliable:

VBA Code:
If Sheets("PQ Cost Source").ListObjects("PQ_Cost_Source").Range.CurrentRegion.Rows.Count > 1 Then   ' The header row counts
    Msgbox "There is data in the table"
End If
 
Upvote 0
Solution
Thank you!

I used:
If Sheets("PQ Cost Source").ListObjects("PQ_Cost_Source").Range.CurrentRegion.Rows.Count > 1 Then ' The header row counts
Msgbox "There is data in the table"
End If
 
Upvote 0

Forum statistics

Threads
1,223,333
Messages
6,171,511
Members
452,407
Latest member
Broken Calculator

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