VBA Subtotals one filter criteria from another column

Davavo

Board Regular
Joined
Aug 3, 2019
Messages
82
I have expenses sheets where various columns of figures need to be totalled according to the filter criteria in the costs centre column.
I had been trying the subtotal 9/109 function expecting that to work, but it seems you can only subtotal if the column is filtered on its own values.

The user imports data into the sheet and the relevant columns total.
The user can then use the filter to isolate a list of any particular cost centre.
The subtotal should then adjust to sum only the visible values.

This si the code I am using
Code:
ThisWorkbook.Sheets("CCRead").Range("d" & Lastrow + 1) = Application.WorksheetFunction.Subtotal(9, ThisWorkbook.Sheets("CCRead").Range("d2:d" & Lastrow))

this totally doesnt work.
When filtered on column "M", the subtotal stays the same. WHen filtered on column "D" it changes.

A simple formula works fine... =SUBTOTAL(9,D2:D20)

but this is not good to me because the user needs to be able to add unknown number of rows.

Should the subtotal function be acting like this?
Maybe find last row and paste in a formula?


thanks
Thanks
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this


Code:
Sub test()
  Dim lastrow As Long, sh As Worksheet
  Set sh = Sheets("CCRead")
  lastrow = sh.UsedRange.Rows(sh.UsedRange.Rows.Count).Row
  sh.Range("D" & lastrow + 1).Value = WorksheetFunction.Subtotal(9, sh.Range("D2:D" & lastrow))
End Sub
 
Upvote 0
Try this


Code:
Sub test()
  Dim lastrow As Long, sh As Worksheet
  Set sh = Sheets("CCRead")
  lastrow = sh.UsedRange.Rows(sh.UsedRange.Rows.Count).Row
  sh.Range("D" & lastrow + 1).Value = WorksheetFunction.Subtotal(9, sh.Range("D2:D" & lastrow))
End Sub

Hi dante, thanks for your response.
That still has the a same problem of not filtering the results when the table is filtered in the "M" column.
Additionally, the usedrange function sends the result to the lowest part of the sheet that has been used recently, rather than the lowest part of the sheet (the bottom of the table) that is in use when it is called.
Clearcontents does not clear whatever it is that the function is looking at, so it necessitates deleting the previously used rows.

I think aggregate is the way forward. I am going to give that a bash. Thanks for your help.
 
Last edited:
Upvote 0
It worked for me, because you don't try to put the result in a variable before taking it to the filtered sheet, for example:

Code:
Sub test()
  Dim lastrow As Long, sh As Worksheet
  Set sh = Sheets("CCRead")
  lastrow = sh.UsedRange.Rows(sh.UsedRange.Rows.Count).Row
  [COLOR=#0000ff]MsgBox [/COLOR]WorksheetFunction.Subtotal(9, sh.Range("D2:D" & lastrow))
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,600
Members
449,038
Latest member
Arbind kumar

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