VBA or formula - Subtotal of Non-Hidden columns

Finalfight40

Active Member
Joined
Apr 24, 2018
Messages
251
Office Version
365
Platform
Windows
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:

FanofExcel18

New Member
Joined
Jun 7, 2018
Messages
48
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.
 

Finalfight40

Active Member
Joined
Apr 24, 2018
Messages
251
Office Version
365
Platform
Windows
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?
 

FanofExcel18

New Member
Joined
Jun 7, 2018
Messages
48
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.
 

Forum statistics

Threads
1,081,690
Messages
5,360,614
Members
400,592
Latest member
badgergurl

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top