Sumvisible VBA to keep re-calculating

kjsmercier

New Member
Joined
Dec 15, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
HI all: I have a sheet where the columns are visible based on a cell count (that is manually typed in see cell CT1 and 2). I would like the total row (DO5) to update off of visible columns but also recalculate every time I change the number. I am using this VBA Thank you!:
Function SUMVisible(Rg As Range)
Dim xCell As Range
Dim xCount As Integer
Dim xTtl As Double

Application.Volatile
Set Rg = Intersect(Rg.Parent.UsedRange, Rg)

For Each xCell In Rg
If xCell.ColumnWidth > 0 _
And xCell.RowHeight > 0 _
And Not IsEmpty(xCell) _
And IsNumeric(xCell.Value) Then
xTtl = xTtl + xCell.Value
xCount = xCount + 1
End If
Next
If xCount > 0 Then
SUMVisible = xTtl
Else
SUMVisible = 0
End If
End Function
 

Attachments

  • Screenshot 2023-01-06 170512.png
    Screenshot 2023-01-06 170512.png
    24.1 KB · Views: 9

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Sub SUMVisible()
Dim lngRow As Long, lngCol As Long
Dim rngRange As Range

Set rngRange = Range("A1:D20") 'Set your range here

For lngRow = 1 To rngRange.Rows.Count
For lngCol = 1 To rngRange.Columns.Count
If rngRange.Columns(lngCol).EntireColumn.Hidden = FALSE Then
Cells(lngRow, 5).Value = Cells(lngRow, 5).Value + Cells(lngRow, lngCol).Value
End If
Next
Next
End Sub
 
Upvote 0
Sub SUMVisible()
Dim lngRow As Long, lngCol As Long
Dim rngRange As Range

Set rngRange = Range("A1:D20") 'Set your range here

For lngRow = 1 To rngRange.Rows.Count
For lngCol = 1 To rngRange.Columns.Count
If rngRange.Columns(lngCol).EntireColumn.Hidden = FALSE Then
Cells(lngRow, 5).Value = Cells(lngRow, 5).Value + Cells(lngRow, lngCol).Value
End If
Next
Next
End Sub
Is this in replacement or in addition to?
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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