Calc Sub total (or Grand tot) of a column of unknown length

ElBombay

Board Regular
Joined
Aug 3, 2005
Messages
196
Hi,

I'm trying to load the CSV version of a monthly report and insert SUBTOTALS and a Grand Total in palces that will obviously change each month. The Data Import and removal of unneeded headers work fine but for now I'm adding the SUBTOTAL() function manually. The module below adds the single and double lines when I comment out the "ActiveCell.Value =" but crashes when I remove the comment-mark. I suppose the answer would allow me to insert whatever formula I need (=STR(), =MID(), etc.

As an additional point, I'm wondering if there is a way to read the current column letter and include that in the assignment at run-time. The columns (G and H in this case) won't change but I think it would would be more elegant if they weren't hard-coded. Thanks, as always

Code:
Sub Add_ChkSum()
'
' Add_ChkSum Macro
' Macro recorded 9/15/2006 by Jim
'

    Range("G609").Select        ' 2 Right of 'EOF'
    ActiveCell.Value = "=SubTotal(G" & Str(iTopDataRow) & ":G" & _
        Str(ActiveCell.Row - 1) & ")"
    
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlDouble
        .Weight = xlThick
        .ColorIndex = xlAutomatic
    End With
    Range("H609").Select
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi,

I was trying to make my question clearer and saw the error. I found the 2nd procedure below (summing()) that was posted by 'Onlyadraftsman' on 8/8/06. I modiifed my code to match his approach (such as using the .Formula object instead of .Value) but still couldn't learn anything from the error msg ("Application-defined or object-defined error"). Then I noticed that I left out the 1st argument (9 for SUMs) needed by the SUBTOTAL function. This time the list helped by forcing me to examine my work. Hope I didn't waste too much of anybody else's time. Thanks :rolleyes:
Code:
Sub Add_ChkSum()
'
' Add_ChkSum Macro
' Macro recorded 9/15/2006 by Jim
'

    Range("G607").Select      ' 2 Right of 'EOF'
    iTopdatarow = 5           ' Set elsewhere in module; added here for my question
    iLastRow = ActiveCell.Row - 1
    
     Range("G607").Formula = "=SubTotal(9,G" & iTopdatarow & ":G" & iLastRow & ")"

   
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlDouble
        .Weight = xlThick
        .ColorIndex = xlAutomatic
    End With
    Range("H609").Select
    
End Sub

Sub summing()
For MY_ROWS = Range("D65536").End(xlUp).Row To 2 Step -1
    If Range("D" & MY_ROWS).Value <> Range("D" & MY_ROWS - 1).Value Then
        Rows(MY_ROWS).EntireRow.Insert

    End If
Next MY_ROWS

For MY_ROWS = Range("A65536").End(xlUp).Offset(1, 0).Row To 1 Step -1
    If Range("b" & MY_ROWS).Value = "" Then
        Range("B" & MY_ROWS).Formula = "=SUM(B" & MY_ROWS - 1 & ":B" & Range("B" & MY_ROWS).End(xlUp).End(xlUp).Row & ")"
    End If
Next MY_ROWS
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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