# Sum Visible Columns or Rows in Range

I came across this user defined fuction on this site.  I would like to perhaps modify the below function to work like the SUM function.  For example, if my formula says =Sumvisible(E2, F2, H2, L2), it would add those cells that are visible.  If I hide Column F, then my formula should recalculate with E2, H2, and L2.  Is this possible or is there a better solution?

You can use user-defined function.
Press Alt-F11 (open VBA editor), add module and copy-paste next code:
Public Function SumVisible(a As Range) As Double
Dim c As Range
Dim value As Double
value = 0

For Each c In a
If c.EntireColumn.Hidden = False And c.EntireRow.Hidden = False Then
If IsNumeric(c.value) = True Then
value = value + c.value
End If
End If
Next
SumVisible = value
End Function

after that on your sheet you can use this function:
=SumVisible("A1:D1")
=SumVisible("B1:B20")

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
The function you posted already does just that.

The only thing I'm finding strange is that it doesn't recalculate when you hide a column. Not even if I press F9. Only when I change one of the values in the range or even the formula.

But asides from that the function is doing what you asked.

Paulo
0
Commented:
If you have a filtered list (Data|Filter|Autofilter), then use the SUBTOTAL function #9. Otherwise the function above must be adapted. Please state whether the ranges G2 and I2:K2 contains numbers.
0
Author Commented:
The function that I posted will sum only visible columns or rows if they are consecutive.  For example, =sumvisible(A1:D1) sums cells A1, B1, C1 and D1 only if not hidden.  I want to have the function work on non-consecutive columns or rows so for example, =sumvisible(A1, D1, F1, Z1) would sum cells A1, D1, F1 and Z1 only if if they are not hidden.

I forgot to add the appilaction.volatile so the function looks like this and the F9 key works:

Public Function SumVisible(a As Range) As Double
Dim c As Range
Dim value As Double
Application.Volatile
value = 0

For Each c In a
If c.EntireColumn.Hidden = False And c.EntireRow.Hidden = False Then
If IsNumeric(c.value) = True Then
value = value + c.value
End If
End If
Next
SumVisible = value
End Function

---------------------

To answer the other comment about G2 and I2:K2 do contain numbers and/or blanks.

0
Commented:
Sorry for the delay.

And sorry for not having tested the formula properly in my first comment. The thing is that if you use the , (which is the union range operator) Excel thinks it's a new argument to the function. The way to deal with this is letting Excel think that this function can take multiple arguments. But we don't really know how many. So here's a first try on this:

Public Function SumVisible(ParamArray a() As Variant) As Double
Dim rng As Variant
Dim value As Double
Application.Volatile
value = 0

For Each rng In a
If Not TypeName(rng) = "Range" Then
value = ""
Exit For
End If
For Each c In rng.Cells
If c.EntireColumn.Hidden = False And c.EntireRow.Hidden = False Then
If IsNumeric(c.value) = True Then
value = value + c.value
End If
End If
Next
Next
SumVisible = value
End Function

As you can see this function now gets an array of arguments. Which means that it can have a variable number of arguments. Then each one is tested for being a Range and then the sum is calculated for each one as it was when you sent it only one Range.

I haven't found any bugs in it. You can even pick up the ranges by using the formula wizard and selecting them in the Worksheet. Just one thing though: If your list separator is defined in your regional settings as the ; and not the , then you'll need to separate multiple ranges with ; instead of ,

So you can either use it like

=Sumvisible(A1:C1;E1:H1)
Or
=Sumvisible(A1:C1,E1:H1)

Hope this helps

Paulo
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
pauloaguia, that is truly neat. Did not find this trick in the net, yet.

BTW, this hit came up, is only closely related, but could be useful: http://www.cof.orst.edu/net/software/excel/tips/#SelectVisible
0
Author Commented:
Thank you.  Excellent!  It works great.
0
Commented:
Thansk for the points. This is one of those questions I enjoyed because I learnt something from it...
0
Commented:
More on this:

Your original function was OK after all. Just found out today that all you needed was to use it like so:

=SUMVisible((A1:C1;F1:I1))
OR
=SUMVisible((A1:C1,F1:I1))

(depending on regional settings)

Just imagine. If I'd known this in the first place we'd never have found out about that ParamArray thing... :)
And now we've learned that you can use () to define combined ranges.... neat :)
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.