Code to drag a subtotal formula to the right

MyAdventureHat

New Member
Joined
Sep 17, 2014
Messages
4
Hello,
I have a list of expenditures that gets longer as time goes on. I used the following code to select the last cell with data in column J, skip two rows down, and add a subtotals formula. Now I want to drag the formula to the right, but can’t seem to set it up right. The starting row will always be different but the starting column will be “J”. I tried using the variable “LastColumn” to autofill the formula but need a starting point for the range. Thank you for your help! :confused::confused:
Private Sub AddSubtotals()
Dim BottomCell As Range
Dim strSubtotalFormula As String
Dim LastColumn As Long

With ActiveSheet
LastColumn = Range("1" & Columns.Count).End(xlToLeft).Column
End With
Set BottomCell = Range("J2").End(xlDown)
BottomCell.Select
ActiveCell.Offset(2, 0).Select
strSubtotalFormula = _
"=subtotal(9,J2:" & BottomCell.Address(False, False) & ")"
ActiveCell.Formula = strSubtotalFormula
Selection.AutoFill Destination:=Range("Don’t know what to put here" & LastColumn)
 

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