Need help with this VBA for Subtotal

MikeL

Active Member
Joined
Mar 17, 2002
Messages
488
Office Version
  1. 365
Platform
  1. Windows
Hello,
Got stuck as I running the VBA below incorrectly replaces the subtotal. I would like to modify the subtotal to be based off col D. Any thoughts?

Sub Test()
'Subtotal Formula
currow = ActiveCell.Row
firstrow = ActiveCell.End(xlUp).End(xlUp).Row
diff = currow - firstrow

For Each cell In Range("d1:d" & Cells(Rows.Count, "d").End(xlUp).Row)
If InStr(cell.Text, "Subtotal") > 0 Then

cell.Offset(0, 5).Select
ActiveCell.FormulaR1C1 = "=SUBTOTAL(9,R[-" & diff & "]C:R[-1]C)"


End If
Next cell
MsgBox ("complete")
End Sub


--------------------------
Fruit and Veggies4.xlsm
ABCDEFGHI
1VEGETABLES
2BEANS5,000
3
4CARROTS6,000
5Subtotal VEGGIES11,000
6
7
8
9FRUIT
10BERRIES5,000
11
12ORANGES6,000
13
14KIWI700
15
16APPLES1,000
17Subtotal FRUIT12,700
Sheet1
Cell Formulas
RangeFormula
I5I5=SUBTOTAL(9,I2:I4)
I17I17=SUBTOTAL(9,I10:I16)
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi. Please, try this code.

VBA Code:
Sub Test()
 Dim rng As Range, c As Range, fst As String, k As Long
  Set rng = Range("D2:D" & Cells(Rows.Count, 4).End(3).Row)
  Set c = rng.Find("Subtotal*") 
   If Not c Is Nothing Then
    fst = c.Address: k = [I1].End(4).Row
    Do
     c.Offset(, 5).Formula = "=SUBTOTAL(9,I" & k & ":I" & c.Row - 1 & ")"
     k = Cells(c.Row, "I").End(4).Row
     Set c = rng.FindNext(c)
    Loop While Not c Is Nothing And c.Address <> fst
   End If
End Sub
 
Upvote 0
Hi. Please, try this code.

VBA Code:
Sub Test()
 Dim rng As Range, c As Range, fst As String, k As Long
  Set rng = Range("D2:D" & Cells(Rows.Count, 4).End(3).Row)
  Set c = rng.Find("Subtotal*")
   If Not c Is Nothing Then
    fst = c.Address: k = [I1].End(4).Row
    Do
     c.Offset(, 5).Formula = "=SUBTOTAL(9,I" & k & ":I" & c.Row - 1 & ")"
     k = Cells(c.Row, "I").End(4).Row
     Set c = rng.FindNext(c)
    Loop While Not c Is Nothing And c.Address <> fst
   End If
End Sub
Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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