Error handling an empty filtered range.

Gringoire

Board Regular
Joined
Nov 18, 2016
Messages
71
Office Version
  1. 365
Platform
  1. Windows
Hello everybody,

I have to obtain a sum from some filtered cells (see code below). Unfortunately the code below raises a 1004 error when the result of filtering operation is an empty range.
I would like to avoid using "On error..." procedures because it could mask some other unexpected error.
I would also like to avoid to use sumifs() instead of filters because I need to put multiple conditions on some columns and that needs a big amount of workaround code...

is there a way to verify the filtered range without raising an error if it is empty?
Statements like :
VBA Code:
If Not (.SpecialCells(xlCellTypeVisible)) Is Nothing
or
VBA Code:
If IsError (.SpecialCells(xlCellTypeVisible))
seems not working...

My code:
VBA Code:
Function filteredsum()
    Dim risultati(1 To 2)
    With shRawData.ListObjects("TabRawData").DataBodyRange
        If Not (.SpecialCells(xlCellTypeVisible)) Is Nothing Then
            risultati(1) = WorksheetFunction.Sum(.Columns(RWC.valtot).SpecialCells(xlCellTypeVisible))
            risultati(2) = WorksheetFunction.Sum(.Columns(RWC.qta).SpecialCells(xlCellTypeVisible))
        Else
            risultati(1) = 0
            risultati(2) = 0
        End If
    End With
    filteredsum = risultati
End Function

thanks.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I solved this issue thanks to this old post:

modified code:
VBA Code:
Function filteredsum()
Dim risultati(1 To 2)
With shRawData.ListObjects("TabRawData").DataBodyRange
If Not .EntireRow.Hidden Then Then
risultati(1) = WorksheetFunction.Sum(.Columns(RWC.valtot).SpecialCells(xlCellTypeVisible))
risultati(2) = WorksheetFunction.Sum(.Columns(RWC.qta).SpecialCells(xlCellTypeVisible))
Else
risultati(1) = 0
risultati(2) = 0
End If
End With
filteredsum = risultati
End Function
 
Upvote 0
It looks like the only time If Not .EntireRow.Hidden Then will evaluate to True is when the data is not filtered.

Also, On Error Resume Next will mask other unexpected errors only when used improperly.

Try the following...

VBA Code:
Function filteredsum()
    Dim filteredRange As Range
    Dim risultati(1 To 2)
    With shRawData.ListObjects("TabRawData").DataBodyRange
        On Error Resume Next
        Set filteredRange = .SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not filteredRange Is Nothing Then
            risultati(1) = WorksheetFunction.Sum(.Columns(RWC.valtot).SpecialCells(xlCellTypeVisible))
            risultati(2) = WorksheetFunction.Sum(.Columns(RWC.qta).SpecialCells(xlCellTypeVisible))
        Else
            risultati(1) = 0
            risultati(2) = 0
        End If
    End With
    filteredsum = risultati
End Function

Hope this helps!
 
Upvote 1
Solution
You are right about both points:
- .EntireRow.Hidden does not work properly
- on error statement if used properly, as you did, do not cause other issues.

thanks a lot!
 
Upvote 0
You're very welcome, and thanks for the feedback.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,854
Members
449,051
Latest member
excelquestion515

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