Subtotal Macro but keep existing formula for one column

Emb21

Board Regular
Joined
Jul 8, 2004
Messages
152
Greetings,

Please see below subtotal macro, I just want to sum columns 7 thru 10 and 12 but col 11 should use existing formula =IF(G10<>0,J10/G10," N/A"). Any help would be appreciated :)

Sub aSubTotal() 'Excel VBA to create subtotals.
Dim iCol As Integer
Dim i As Integer
Dim j As Integer

Application.ScreenUpdating = False
i = 10
j = i
'Sort the data so like data is grouped together.
Range("D10").CurrentRegion.Offset(1).Sort Range("D11"), 1
'Loops throught Col D Checking for match then when there is no match add Sum
Do While Range("D" & i) <> ""
If Range("D" & i) <> Range("D" & (i + 1)) Then
Rows(i + 1).Insert
Range("D" & (i + 1)) = "Subtotal " & Range("D" & i).Value
For iCol = 7 To 12 'Columns to Sum
Cells(i + 1, iCol).Formula = "=SUM(R" & j & "C:R" & i & "C)"
Next iCol
Range(Cells(i + 1, 1), Cells(i + 1, 12)).Font.Bold = True
i = i + 2
j = i
Else
i = i + 1
End If
Loop

Thank you,
Eric
 

Attachments

  • example.JPG
    example.JPG
    52.5 KB · Views: 10

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I added a check to see if iCol = 11, make it 12 before the formula is set.

VBA Code:
Sub aSubTotal() 'Excel VBA to create subtotals.
    Dim iCol As Integer
    Dim i As Integer
    Dim j As Integer
    
    Application.ScreenUpdating = False
    i = 10
    j = i
    'Sort the data so like data is grouped together.
    Range("D10").CurrentRegion.Offset(1).Sort Range("D11"), 1
    'Loops throught Col D Checking for match then when there is no match add Sum
    Do While Range("D" & i) <> ""
        If Range("D" & i) <> Range("D" & (i + 1)) Then
            Rows(i + 1).Insert
            Range("D" & (i + 1)) = "Subtotal " & Range("D" & i).Value
            For iCol = 7 To 12 'Columns to Sum
                If iCol = 11 Then
                    iCol = 12
                End If
                Cells(i + 1, iCol).Formula = "=SUM(R" & j & "C:R" & i & "C)"
            Next iCol
            Range(Cells(i + 1, 1), Cells(i + 1, 12)).Font.Bold = True
            i = i + 2
            j = i
        Else
            i = i + 1
        End If
    Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

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