Using UDF to Sum only Visible Cells

Jerry Sullivan

MrExcel MVP
Joined
Mar 18, 2010
Messages
8,787
I'm trying to implement Chip Pearson's isVisible UDF to Sum only visible cells.
http://www.cpearson.com/excel/IsVisible.aspx

I'm finding that the Array formula recalculates automatically when Rows are Hidden and Unhidden.

However the Array formula does not recalculate when Columns are Hidden or Unhidden.

Excel Workbook
ABCDEFGH
121123456
Before Hiding D-F


Excel Workbook
ABCGH
121156
After Hiding D-F


In fact, even if I click Calculate Now (F9), the value does not recalculate.
The formula recalculates if I change one of the inuput variables change, or if Application.Volatile is added to the UDF the formula recalculates on any change to the worksheet.

Strangely, Formula Auditing > Evaluate Formula shows this analysis:
SUM(isVisible(C1:H1)*C1:H1)
SUM({TRUE,FALSE,FALSE,FALSE,TRUE,TRUE}*C1:H1)
SUM({1,0,0,0,5,6})
21 :confused:

Does anyone know why this is happening and a possible fix?

Note that my Columns are Hidden (not Filtered) so SubTotal will not work.

Thanks in advance!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi M8,

Try UDF below non array answer

Code:
Function Sum_Visible_Cells(Cells_To_Sum As Object)
       Dim cell As Range
       Dim Total As Double
       Total = 0
       Application.Volatile
       For Each cell In Cells_To_Sum
           If cell.Rows.Hidden = False Then
               If cell.Columns.Hidden = False Then
                   Total = Total + cell.Value
               End If
           End If
       Next
       Sum_Visible_Cells = Total
End Function

Cell A1 =Sum_Visible_Cells(C1:H1)

Biz
 
Upvote 0
Hiding rows causes a recalc (because subtotal needs it to), hiding columns does not. Why not have the udf calc the sum?
 
Upvote 0
Hiding rows causes a recalc (because subtotal needs it to), hiding columns does not. Why not have the udf calc the sum?

Ahhaaa... That explains it why. Rorya and Biz, thank you both for your suggestions.

I need to apply the same concept to some lease payment calculations, so I was hoping to use the more general-purpose isVisible UDF instead of bulding the payment calculations into the UDF. (I figured I'd start simple with a SUM function and work my way up...not expecting to hit this wall). :)

I had the same problem with Biz' UDF not recalculating upon Columns being Hidden/Unhidden. I might try adding a benign change to one of the dependent cells to see if that triggers a recalc.
 
Upvote 0
Ahhaaa... That explains it why. Rorya and Biz, thank you both for your suggestions.

I need to apply the same concept to some lease payment calculations, so I was hoping to use the more general-purpose isVisible UDF instead of bulding the payment calculations into the UDF. (I figured I'd start simple with a SUM function and work my way up...not expecting to hit this wall). :)

I had the same problem with Biz' UDF not recalculating upon Columns being Hidden/Unhidden. I might try adding a benign change to one of the dependent cells to see if that triggers a recalc.


Press F9 to force UDF to calculate.

Biz
 
Upvote 0
Since I'm hiding columns for various customers using VBA,
I will probably use the UDF (either isVisible or one that builds in the Interest Payments),
and add Range.Calculate to trigger a calculation of any cells with the UDF.

Code:
Sub HideColumns()
    Range("D:E").EntireColumn.Hidden = True
    Range("A1").Calculate
End Sub

Thanks again Biz and Rorya for pointing me in the right direction. :)
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,875
Members
452,949
Latest member
Dupuhini

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