Subtotal like autosum with macro button; Selection as relative range in VBA

Rowland Hamilton

Active Member
Joined
Nov 13, 2009
Messages
250
Folks,

Subtotal is superior to Sum, yadda yadda yadda. But Sum has a convenient button. When you click it, it knows to highlight the contiguous range above or to the left. I want to create a macro button for Subtotal that does the same thing, without the subtotal groupings.

How can I make the selection range read as a partial relative reference range with locked rows not columns, or at least a full relative range, so I can drag the resulting formula to the right?

I found this macro that comes close, performing the action on the selection, not the contiguous range:

Code:
Sub SUBTOTAL_Range()
    Set Rng = Selection
    Set rng1 = Rng.Offset(Rng.Rows.Count, 0).Resize(1, 1)
    rng1.Formula = "=Subtotal(9," & Rng.Address & ")"
End Sub

Besides the shortcoming of having to highlight the range (which is actually helpful when I was pulling data into my P&L template with sumif and adding subtotals beneath my ranges), my problem is that the ranges are absolute so I can't drag the formulas over.

Thank you, Rowland Hamilton
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi.

for row abs and column relative
Code:
rng1.Formula = "=Subtotal(9," & Rng.Address(, 0) & ")"

for both relative
Code:
rng1.Formula = "=Subtotal(9," & Rng.Address(0, 0) & ")"
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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