Improper subtotals using macro recorder

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
112
Office Version
  1. 2019
Platform
  1. Windows
Hi. I have here a macro for making subtotals for a set of data. However when I run the code, it does not give the proper subtotals.

See below the result when I run the code:

1597560974405.png




1597561075004.png


I also attached the code for reference:
VBA Code:
Sub SubtotalMacro()
'
Dim Lastrow As Long

Sheets(1).Activate
Lastrow = Range("A" & Rows.Count).End(xlUp).Row

Range("A1:D" & Lastrow).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(3, 4), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(3, 4), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I believe that your problem lies in the use of 'Selection' (which the macro recorder gives you but in general is a bad idea as it is mostly not necessary and slows your code)

Your code initially selects A1:D8
However once the first subtotal is done, the data now occupies more than 8 rows but you still only have 8 rows selected.
So when the second subtotal is applied so it only applies to 8 rows.

Try this instead
VBA Code:
Sub SubtotalMacro_v2()
  Sheets(1).Activate
  Range("A1:D" & Range("A" & Rows.Count).End(xlUp).Row).Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(3, 4), Replace:=False, PageBreaks:=False, SummaryBelowData:=True
  Range("A1:D" & Range("A" & Rows.Count).End(xlUp).Row).Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(3, 4), Replace:=False, PageBreaks:=False, SummaryBelowData:=True
End Sub
 
Upvote 0
Does it mean that the variable "Lastrow" will ALWAYS have a value of 8 throughout the code? (It will not adjust whether I add or delete rows of data on Column A?)
 
Upvote 0
Does it mean that the variable "Lastrow" will ALWAYS have a value of 8 throughout the code?
Yes, unless you recalculate it along the way like this (though I am not recommending this approach)

VBA Code:
Sub SubtotalMacro_v3()
  Dim Lastrow As Long
  
  Sheets(1).Activate
  
  Lastrow = Range("A" & Rows.Count).End(xlUp).Row
  Range("A1:D" & Lastrow).Select
  Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(3, 4), Replace:=False, PageBreaks:=False, SummaryBelowData:=True
  
  Lastrow = Range("A" & Rows.Count).End(xlUp).Row
  Range("A1:D" & Lastrow).Select
  Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(3, 4), Replace:=False, PageBreaks:=False, SummaryBelowData:=True
End Sub
 
Upvote 0
Yes, unless you recalculate it along the way like this (though I am not recommending this approach)

VBA Code:
Sub SubtotalMacro_v3()
  Dim Lastrow As Long
 
  Sheets(1).Activate
 
  Lastrow = Range("A" & Rows.Count).End(xlUp).Row
  Range("A1:D" & Lastrow).Select
  Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(3, 4), Replace:=False, PageBreaks:=False, SummaryBelowData:=True
 
  Lastrow = Range("A" & Rows.Count).End(xlUp).Row
  Range("A1:D" & Lastrow).Select
  Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(3, 4), Replace:=False, PageBreaks:=False, SummaryBelowData:=True
End Sub
Ohh okay. The one you initially provided is much better than this lol anyway thanks for explaining. Have a nice day!
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,284
Members
448,885
Latest member
LokiSonic

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