Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-19-2023, 02:39 PM
p45cal's Avatar
p45cal p45cal is offline Sum visible rows Windows 10 Sum visible rows Office 2021
Expert
 
Join Date: Apr 2014
Posts: 948
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

A selection of macros to put the formulae in place:
Code:
Sub blah()
Selection.Rows(1).Offset(-1).Formula = "=SUBTOTAL(109," & Selection.Columns(1).Address(0, 0) & ")"
End Sub
Code:
Sub blah2()
With Selection.Rows(1).Offset(-1)
  .Formula = "=SUBTOTAL(109," & Selection.Columns(1).Address(0, 0) & ")"
  .Value = .Value 'optional line to convert the formulae to plain values.
End With
End Sub
The first macro will put fomulae in the cells and their values will adjust immediately to what's hidden/not hidden.

In the second macro, if you remove the .Value = .Value line the same as above,
but if you leave that line in, they'll become plain values and will not adjust to hidden/not hidden rows, unless the macro is run again.

edit:
and another, more akin to what you originally asked:
Code:
Sub blah4()
Set visRng = Selection.Columns(1).SpecialCells(xlCellTypeVisible)
Selection.Rows(1).Offset(-1).Formula = "=sum(" & visRng.Address(0, 0) & ")"
End Sub

Reply With Quote
  #2  
Old 06-19-2023, 09:00 PM
soroush.kalantari soroush.kalantari is offline Sum visible rows Windows 10 Sum visible rows Office 2016
Competent Performer
Sum visible rows
 
Join Date: Jun 2021
Posts: 124
soroush.kalantari is on a distinguished road
Default

Quote:
Originally Posted by p45cal View Post
A selection of macros to put the formulae in place:
Code:
Sub blah()
Selection.Rows(1).Offset(-1).Formula = "=SUBTOTAL(109," & Selection.Columns(1).Address(0, 0) & ")"
End Sub
Code:
Sub blah2()
With Selection.Rows(1).Offset(-1)
  .Formula = "=SUBTOTAL(109," & Selection.Columns(1).Address(0, 0) & ")"
  .Value = .Value 'optional line to convert the formulae to plain values.
End With
End Sub
The first macro will put fomulae in the cells and their values will adjust immediately to what's hidden/not hidden.

In the second macro, if you remove the .Value = .Value line the same as above,
but if you leave that line in, they'll become plain values and will not adjust to hidden/not hidden rows, unless the macro is run again.

edit:
and another, more akin to what you originally asked:
Code:
Sub blah4()
Set visRng = Selection.Columns(1).SpecialCells(xlCellTypeVisible)
Selection.Rows(1).Offset(-1).Formula = "=sum(" & visRng.Address(0, 0) & ")"
End Sub
Thank for your reply, blah4 can do what I want.
Reply With Quote
Reply

Tags
formula, visible



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sum visible rows Add Rows to table that will include content controls of previous rows bobsagat Word VBA 20 01-27-2020 08:00 AM
VBA color of visible gridlines of tables word (with some merged cells in first rows) Alvaro.passi Word VBA 0 07-18-2017 09:11 AM
How to find duplicate rows in a huge excel file containing 500000 rows Stc25234 Excel Programming 2 06-23-2017 10:51 AM
Sum visible rows Delete All empty Rows - Print - Undo all Rows deleted Bathroth Word VBA 1 10-01-2014 01:40 PM
Sum visible rows Grouping table rows to prevent individual rows from breaking across pages dennist77 Word 1 10-29-2013 11:39 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:55 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft