Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default How to use Subtotal in VBA

I have a spreadsheet used to store data collected and plan to write some VBA
codes associate to a button. I have a test routine using the Filter function
as shown in sample below

Selection.AutoFilter Field:=1, Criteria1:="MLI"
Selection.AutoFilter Field:=6, Criteria1:="Cost Performance Index"
Selection.AutoFilter Field:=7, Criteria1:="Computed Metric"

Can someone please tell me how can use the "Subtotal" function as if it were
on the spreadsheet to sum and average of the above filtered data stored in
column J ?

Thanks in advance for any assistance


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default How to use Subtotal in VBA

Hi Wellie;

Try something like:

Sub Tester()
Dim rng As Range
Dim MyTotal As Double
Dim MyAverage As Double
Const strCol As String = "B"

Set rng = ActiveSheet.AutoFilter.Range

With rng
.AutoFilter Field:=1, Criteria1:="MLI"
.AutoFilter Field:=6, Criteria1:="Cost Performance Index"
.AutoFilter Field:=7, Criteria1:="Computed Metric"
End With

With Application
MyTotal = .Subtotal(9, rng.Columns(strCol))
MyAverage = Format(.Subtotal(1, rng.Columns(strCol)), "0.00")
End With

MsgBox "Filter Total= " & MyTotal & vbNewLine _
& "Filter Average = " & MyAverage

End Sub

Note that this includes no error checking.

---
Regards,
Norman



"Wellie" wrote in message
...
I have a spreadsheet used to store data collected and plan to write some
VBA
codes associate to a button. I have a test routine using the Filter
function
as shown in sample below

Selection.AutoFilter Field:=1, Criteria1:="MLI"
Selection.AutoFilter Field:=6, Criteria1:="Cost Performance Index"
Selection.AutoFilter Field:=7, Criteria1:="Computed Metric"

Can someone please tell me how can use the "Subtotal" function as if it
were
on the spreadsheet to sum and average of the above filtered data stored in
column J ?

Thanks in advance for any assistance




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default How to use Subtotal in VBA

Hi Wellie,

Const strCol As String = "B"


Should read:

Const strCol As String = "J"

---
Regards,
Norman



"Norman Jones" wrote in message
...
Hi Wellie;

Try something like:

Sub Tester()
Dim rng As Range
Dim MyTotal As Double
Dim MyAverage As Double
Const strCol As String = "B"

Set rng = ActiveSheet.AutoFilter.Range

With rng
.AutoFilter Field:=1, Criteria1:="MLI"
.AutoFilter Field:=6, Criteria1:="Cost Performance Index"
.AutoFilter Field:=7, Criteria1:="Computed Metric"
End With

With Application
MyTotal = .Subtotal(9, rng.Columns(strCol))
MyAverage = Format(.Subtotal(1, rng.Columns(strCol)), "0.00")
End With

MsgBox "Filter Total= " & MyTotal & vbNewLine _
& "Filter Average = " & MyAverage

End Sub

Note that this includes no error checking.

---
Regards,
Norman



"Wellie" wrote in message
...
I have a spreadsheet used to store data collected and plan to write some
VBA
codes associate to a button. I have a test routine using the Filter
function
as shown in sample below

Selection.AutoFilter Field:=1, Criteria1:="MLI"
Selection.AutoFilter Field:=6, Criteria1:="Cost Performance Index"
Selection.AutoFilter Field:=7, Criteria1:="Computed Metric"

Can someone please tell me how can use the "Subtotal" function as if it
were
on the spreadsheet to sum and average of the above filtered data stored
in
column J ?

Thanks in advance for any assistance






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subtotal To Include Item Description On Subtotal Line Tickfarmer Excel Discussion (Misc queries) 2 February 23rd 10 07:56 PM
sort macro, subtotal and add lines after subtotal David Excel Discussion (Misc queries) 1 August 29th 09 10:56 AM
pasting to subtotal lines without replacing hidden -non-subtotal l harleydiva67 Excel Discussion (Misc queries) 1 October 12th 06 06:02 PM
Subtotal of Subtotal displays Grand Total in wrong row Thomas Born Excel Worksheet Functions 5 January 6th 05 01:46 PM
Sort, Subtotal, Label Subtotal, Insert row Teak Excel Programming 2 April 8th 04 04:14 PM


All times are GMT +1. The time now is 11:37 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"