Subtotal function to last row in data query (VBA)

Markylex

New Member
Joined
Jun 30, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I'm currently working on a VBA macro and have a query associated to subtotalling data within one column, down to the last row that contains data within my spreadsheet. The idea is that this macro is dynamic, as the source data will change in volume and therefore value as well.

At the moment, this is the VBA code I have:

Dim LastRow as Long
LastRow = Range("O6").End(xlDown).Row
Range("O4").Select
ActiveCell.FormulaR1C1 = "=SUBTOTAL(9,O7:O" & LastRow & ")"

The above should work by finding the last row in my data, starting from O6. I then subtotal all the data from O7 to the last row of my data, with the formula being located in O4.

With the current data set I am using, my last row is 590. The subtotal does 'work', however it seems to go to line 594 instead. What confuses me even more, is that when I run the macro line by line, it does seem to pick up 590 as the value in 'LastRow'. I've also tried setting LastRow by using rows.count, rather than xlDown, however the end result is the same.

I'd still consider myself quite a 'newbie' when it comes to VBA, so please bare with me if the issue is an obvious one.

Should you require any further information, such as screenshots of what the actual spreadsheet looks like or any further details on the other sections of macro code, let me know.

Thanks in advance for any support you can provide!
 
OK, I think I see the problem. I missed it the first time around, but I see it now.
You only use ".FormulaR1C1" if you are using relative range referencing in your formula, which you are not.
So you should just be using ".Formula", i.e.

VBA Code:
Range("O4").Formula = "=SUBTOTAL(9,O7:O" & LastRow & ")"
 
Upvote 0
Solution

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Apologies for the delay in responding, it's been a rather manic week for me.

I changed the subtotal line back to what I had when I first created this thread, as I had actually amended it to use relative range referencing at the point where I shared the procedure with you. After changing it back to the original code, I then adjusted the line to use .Formula, rather than .FormulaR1C1 and it worked!

Thanks for your support Joe, it's much appreciated! :)
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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