Check if range is empty (Visible Cells)

Joker4321

Board Regular
Joined
Jan 5, 2007
Messages
60
Hi I have am using an auto filter and looping through the visible cells that remain. I get an error when there is no data visible based on the filtered criteria. I have the following code...

With Sheets("DB")
Set TableRange = .Range(.Cells(Origin.Row + 3, Origin.Column + x), .Cells(End.Row - 1, Origin.Column + x))
End With

For Each First In TableRange.SpecialCells(xlCellTypeVisible)


is there a way to check if TableRange.SpecialCells(xlCellTypeVisible)
is null. Basically I want to skip the For statement if there are no visible cells after the data is filtered. Thanks a lot.
 
Except EntireRow.Hidden didn't work in my special case for ListObject data range (table).

However since we always have a header, we can use following solution in my case to copy filtered data range in a table:
(tbl is a ListObject object)

VBA Code:
If tbl.ListColumns(1).Range.SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
  'There are certainly visible cells filtered.
  tbl.DataBodyRange.SpecialCells(xlCellTypeVisible).Copy destinationRange
End If
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Except EntireRow.Hidden didn't work in my special case for ListObject data range (table).

However since we always have a header, we can use following solution in my case to copy filtered data range in a table:
(tbl is a ListObject object)

VBA Code:
If tbl.ListColumns(1).Range.SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
  'There are certainly visible cells filtered.
  tbl.DataBodyRange.SpecialCells(xlCellTypeVisible).Copy destinationRange
End If

It appears that only the first row in the range will be evaluated when you use EntireRow.Hidden with a range of multiple rows. That's what this solution appears to be doing in my code.
 
Upvote 0
I was also looking for an easy way to avoid the error when working with a ListObject, and found that combining the ideas of two previous posts yields a very easy solution:

If Not tbl.DataBodyRange.EntireRow.Hidden Then
'do whatever you need here with tbl.DataBodyRange.SpecialCells(xlCellTypeVisible), which is guaranteed to have at least one area in the range
 
Upvote 0
I was also looking for an easy way to avoid the error when working with a ListObject, and found that combining the ideas of two previous posts yields a very easy solution:

If Not tbl.DataBodyRange.EntireRow.Hidden Then
'do whatever you need here with tbl.DataBodyRange.SpecialCells(xlCellTypeVisible), which is guaranteed to have at least one area in the range
Hmm, no... jimicos is right, so I changed it to this and it seems to work fine now:
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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