VBA or formula - Subtotal of Non-Hidden columns

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Not sure of how to make it recalculate automatically but if you don't get any other options, you could add a button that will refresh the calculations for you when you press it.

The Function would look like:

Code:
[SIZE=2][FONT=arial]Function Sum_Visible_Cells(Cells_To_Sum As Object)[/FONT][/SIZE]
[SIZE=2][FONT=arial]    Application.Volatile[/FONT][/SIZE]
[SIZE=2][FONT=arial]    For Each cell In Cells_To_Sum[/FONT][/SIZE]
[SIZE=2][FONT=arial]        If cell.Rows.Hidden = False Then[/FONT][/SIZE]
[SIZE=2][FONT=arial]            If cell.Columns.Hidden = False Then[/FONT][/SIZE]
[SIZE=2][FONT=arial]                Total = Total + cell.Value[/FONT][/SIZE]
[SIZE=2][FONT=arial]            End If
[/FONT][/SIZE]
[SIZE=2][FONT=arial]        End If[/FONT][/SIZE]
[SIZE=2][FONT=arial]    Next[/FONT][/SIZE]
[SIZE=2][FONT=arial]    Sum_Visible_Cells = Total[/FONT][/SIZE]
[SIZE=2][FONT=arial]End Function[/FONT][/SIZE]


(credit to cyrilbrd)

And you could have another sub which you assign to a button which could be something like this:

Code:
[SIZE=2][FONT=arial]Sub Refresh()[/FONT][/SIZE]

[SIZE=2][FONT=arial]ActiveSheet.EnableCalculation = False[/FONT][/SIZE]
[SIZE=2][FONT=arial]ActiveSheet.EnableCalculation = True[/FONT][/SIZE]
[SIZE=2][FONT=arial]
[/FONT][/SIZE]
[SIZE=2][FONT=arial]End Sub[/FONT][/SIZE]
 
Last edited:
Upvote 0
Not sure of how to make it recalculate automatically but if you don't get any other options, you could add a button that will refresh the calculations for you when you press it.

The Function would look like:

Code:
[SIZE=2][FONT=arial]Function Sum_Visible_Cells(Cells_To_Sum As Object)[/FONT][/SIZE]
[SIZE=2][FONT=arial]    Application.Volatile[/FONT][/SIZE]
[SIZE=2][FONT=arial]    For Each cell In Cells_To_Sum[/FONT][/SIZE]
[SIZE=2][FONT=arial]        If cell.Rows.Hidden = False Then[/FONT][/SIZE]
[SIZE=2][FONT=arial]            If cell.Columns.Hidden = False Then[/FONT][/SIZE]
[SIZE=2][FONT=arial]                Total = Total + cell.Value[/FONT][/SIZE]
[SIZE=2][FONT=arial]            End If
[/FONT][/SIZE]
[SIZE=2][FONT=arial]        End If[/FONT][/SIZE]
[SIZE=2][FONT=arial]    Next[/FONT][/SIZE]
[SIZE=2][FONT=arial]    Sum_Visible_Cells = Total[/FONT][/SIZE]
[SIZE=2][FONT=arial]End Function[/FONT][/SIZE]


(credit to cyrilbrd)

And you could have another sub which you assign to a button which could be something like this:

Code:
[SIZE=2][FONT=arial]Sub Refresh()[/FONT][/SIZE]

[SIZE=2][FONT=arial]ActiveSheet.EnableCalculation = False[/FONT][/SIZE]
[SIZE=2][FONT=arial]ActiveSheet.EnableCalculation = True[/FONT][/SIZE]
[SIZE=2][FONT=arial]
[/FONT][/SIZE]
[SIZE=2][FONT=arial]End Sub[/FONT][/SIZE]

I'm getting #VALUE ! back.
 
Upvote 0
Strange its working for me. Are you entering these in the visual basic workspace and then using:

=Sum_Visible_Cells(b3:ac3)

on the worksheet?
 
Upvote 0
Strange its working for me. Are you entering these in the visual basic workspace and then using:

=Sum_Visible_Cells(b3:ac3)

on the worksheet?

Yes
Here is my VBA:
Function Sum_Visible_Cells(Cells_To_Sum As Object)
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

Total I have for all cells involved is 103, when I hide all but 3 columns it should be 13 (8,0,5). The 103 is showing (I hit calculate on manual, So i accept full responsibility for VALUE), but it would go to 13. I did put the refresh code you suggested, stays at 13.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,193
Members
452,616
Latest member
intern444

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