VBA to sum columns in blank cells

karadeon

New Member
Joined
Oct 25, 2016
Messages
3
Hello!

I'm trying to sum certain columns and for the sum to be placed inside the blanks while adding a row after the sum. I used a macro to fill in the Deposits column (which is column D) and the Mkt Share column (which is column E). However, I would need it to also sum column "Br" (which is column F), "Pen" (which is column G). And I would need a blank row to appear after the sum row. I'll place my code in here.

Thanks for your help!

Sub Subtotal()
Dim lngLastRow As Long, _
lngFormulaRowStart As Long, _
lngFormulaRowEnd As Long
Dim rngCell As Range

lngLastRow = Cells(Rows.Count, "D").End(x1Up).Row + 1
lngFormulaRowStart = 2
lngFormulaRowEnd = 2

Application.ScreenUpdating = False

For Each rngCell In Range("D2:D" & lngLastRow)

If Len(rngCell.Value) = 0 Then

lngFormulaRowEnd = rngCell.Row - 1

rngCell.Formula = "=SUM(D" & lngFormulaRowStart & ":D" & lngFormulaRowEnd & ")"
rngCell.Offset(0, 1).Formula = "=SUM(E" & lngFormulaRowStart & ":E" & lngFormulaRowEnd & ")"

lngFormulaRowStart = rngCell.Offset(1, 0).Row
lngFormulaRowEnd = rngCell.Offset(1, 0).Row

End If

Next rngCell

Application.ScreenUpdating = True

End Sub


CBSABankRankDepositsMkt ShrBrPenDep/Br
Aberdeen, SDWells Fargo & Company (CA)1361,94024.4%313.0%120,647
Aberdeen, SDDacotah Banks, Inc. (SD)2339,16522.8%28.7%169,583
Aberdeen, SDGreat Western Bancorp, Inc. (SD)3172,81411.6%28.7%86,407
Aberdeen, SDPlains Commerce Bank (SD)4157,71610.6%14.3%157,716
Aberdeen, SDU.S. Bancorp (MN)5129,9308.7%14.3%129,930
Aberdeen, SDRoscoe Community Bankshares, Inc. (SD)684,4145.7%28.7%42,207
Aberdeen, SDFirst Bank Shares Corporation (SD)752,9503.6%28.7%26,475
Aberdeen, SDIpswich Community Bancshares, Inc. (SD)845,0643.0%14.3%45,064
Aberdeen, SDFirst State Bank of Claremont (SD)935,9832.4%313.0%11,994
Aberdeen, SDHopkins Financial Corporation (SD)1031,7012.1%28.7%15,851
Aberdeen, SDGreat Plains Bank Corporation (SD)1130,4512.0%14.3%30,451
Aberdeen, SDFirst National Bank of Frederick (SD)1217,8191.2%14.3%17,819
Aberdeen, SDH2H Bancshares, Inc. (SD)1315,5261.0%14.3%15,526
Aberdeen, SDBeresford Bancorporation, Inc. (SD)1410,5060.7%14.3%10,506
1,485,979100.0%
Aberdeen, WAPacific Financial Corporation (WA)1307,47433.4%521.7%61,495
Aberdeen, WATimberland Bancorp, Inc. (WA)2218,23423.7%626.1%36,372
Aberdeen, WAAnchor Bancorp (WA)3206,68622.5%521.7%41,337
Aberdeen, WAUmpqua Holdings Corporation (OR)483,5649.1%28.7%41,782
Aberdeen, WAU.S. Bancorp (MN)530,3613.3%14.3%30,361
Aberdeen, WAKeyCorp (OH)627,3953.0%14.3%27,395
Aberdeen, WAJPMorgan Chase & Co. (NY)721,7952.4%14.3%21,795
Aberdeen, WACascade Bancorp (OR)815,9231.7%14.3%15,923
Aberdeen, WASecurity State Corporation (WA)99,0421.0%14.3%9,042
920,474100.0%
Abilene, TXFirst Financial Bankshares, Inc. (TX)11,219,53646.0%1329.5%93,810
<colgroup><col width="197" style="width: 148pt; mso-width-source: userset; mso-width-alt: 7204;"> <col width="285" style="width: 214pt; mso-width-source: userset; mso-width-alt: 10422;"> <col width="36" style="width: 27pt; mso-width-source: userset; mso-width-alt: 1316;"> <col width="76" style="width: 57pt; mso-width-source: userset; mso-width-alt: 2779;"> <col width="49" style="width: 37pt; mso-width-source: userset; mso-width-alt: 1792;"> <col width="28" style="width: 21pt; mso-width-source: userset; mso-width-alt: 1024;"> <col width="42" style="width: 32pt; mso-width-source: userset; mso-width-alt: 1536;"> <col width="76" style="width: 57pt; mso-width-source: userset; mso-width-alt: 2779;"> <tbody> </tbody>
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi,

I am not 100% sure I know what your requirements are because you are asking for subtotals and you already have some listed. Anyway this code will subtotal columns "F" & "G" and then insert a blank row.

See if it comes close to what you are looking for... Please test on a backup copy of your data.

Code:
Sub GetSubs()


' Add Subtotals
    Application.ScreenUpdating = False
    Dim subt As Double, subt2 As Double
    Dim break As Long
    Dim lastrow As Long


    lastrow = Cells(Rows.Count, "F").End(xlUp).Row
    
nxt:
    break = Cells(lastrow, "F").End(xlUp).Row
    If Cells(lastrow - 1, 6) = "" Then break = lastrow
    subt = WorksheetFunction.Sum(Range("F" & break, "F" & lastrow))
    Range("F" & lastrow).Offset(1, 0).Value = subt
    subt2 = WorksheetFunction.Sum(Range("G" & break, "G" & lastrow))
    Range("G" & lastrow).Offset(1, 0).Value = Format(subt2, "Percent")
    Range("F" & lastrow).Offset(1, 0).Offset(1, 0).EntireRow.Insert
    lastrow = Cells(break, 6).End(xlUp).Row
    If lastrow < 2 Then End
    
    GoTo nxt
    Application.ScreenUpdating = True
    
End Sub

I hope this helps!

igold
 
Upvote 0
This was perfect!!!! Thank you so much for your help!!!

Is it possible do delete groups of rows if a certain name doesn't appear in that group? In the picture I pasted in here, it had Aberdeen, SD and Aberdeen WA. Let's say I wanted to keep the groups of cities that has Anchor Bancorp in that city. Anchor Bancorp appears in Aberdeen, WA but doesn't appear in Aberdeen, SD. Is there a way to delete all the lines of Aberdeen, SD because that specific bank isn't in that city?
 
Upvote 0
This was perfect!!!! Thank you so much for your help!!!

Is it possible do delete groups of rows if a certain name doesn't appear in that group? In the picture I pasted in here, it had Aberdeen, SD and Aberdeen WA. Let's say I wanted to keep the groups of cities that has Anchor Bancorp in that city. Anchor Bancorp appears in Aberdeen, WA but doesn't appear in Aberdeen, SD. Is there a way to delete all the lines of Aberdeen, SD because that specific bank isn't in that city?
 
Upvote 0
Hi karadeon,

Thanks for the feedback, I am glad we got that part working.

I almost forgot... Welcome to MrExcel.

See if this fulfills your new requirement. Please be sure to test on a backup copy of your worksheet, as this code will delete data that you cannot recover.

Code:
Sub GetSubs2()


    'Add Subtotals and remove groups
    Dim bf As Boolean
    Dim subt As Double, subt2 As Double
    Dim break As Long, i As Long, lastrow As Long
    
    Application.ScreenUpdating = False
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    
nextgrp:
    bf = False
    break = Cells(lastrow, "F").End(xlUp).Row
    If break = 1 Then break = 2
    If Cells(lastrow - 1, 6) = "" Then break = lastrow
    For i = break To lastrow
        If Cells(i, 2) = "Anchor Bancorp (WA)" Then
            bf = True
            Exit For
        End If
    Next
    If Not bf Then Range(Cells(break, 1), Cells(lastrow + 1, 8)).Delete
    lastrow = Cells(break, 6).End(xlUp).Row
    If lastrow < 2 Then GoTo nxtsec
    GoTo nextgrp
    
nxtsec:
    lastrow = Cells(Rows.Count, "F").End(xlUp).Row
    
nxt:
    break = Cells(lastrow, "F").End(xlUp).Row
    subt = WorksheetFunction.Sum(Range("F" & break, "F" & lastrow))
    Range("F" & lastrow).Offset(1, 0).Value = subt
    subt2 = WorksheetFunction.Sum(Range("G" & break, "G" & lastrow))
    Range("G" & lastrow).Offset(1, 0).Value = Format(subt2, "Percent")
    Range("F" & lastrow).Offset(1, 0).Offset(1, 0).EntireRow.Insert
    lastrow = Cells(break, 6).End(xlUp).Row
    If lastrow < 2 Then GoTo CleanUp
    GoTo nxt
    
CleanUp:
    Application.ScreenUpdating = True
    
End Sub

Let me know how it goes...

igold
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,031
Members
449,205
Latest member
Eggy66

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