How to use Subtotal in VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top