Find Last Empty Cell in a Column & then Sum all the Cells Above?

jassyphee

New Member
Joined
Jan 8, 2014
Messages
16
Currently the data has blank cells in between, every time I sum of the numbers it only sums the first couple of cells and places the total in the empty cell. So the column A is the headings and description of amounts, and column B is the amount. As well, the number of entries vary all the time, therefore differing rows. I want to do a sum total of column B in the last row of the column (of all those rows). As well, I have multiple sheets running, but this is the only sheet that has blank spaces due to heading issues. The following is the copy of the code:
PHP:
Function calSum(sheetName As String, startCol As String, startRow As Integer, _
                refName As String, sumTitle As String)
    Sheets(sheetName).Activate
    
    Dim row As Integer
    
    row = startRow
     Do While Not IsEmpty(Range(startCol & row))
        row = row + 1
    Loop
    
    
    Range(startCol & row).Formula = "=sum(" & startCol & startRow & ":" & startCol & row - 1 & ")"
    With Range(startCol & row).Borders(xlEdgeTop)
        .LineStyle = xlContinuous
    End With
    With Range(startCol & row).Borders(xlEdgeBottom)
        .LineStyle = xlDouble
    End With
    
    
    Range(startCol & row).Offset(0, -1).Value = sumTitle
    Range(startCol & row).Offset(0, -1).Font.Bold = True
    
    
    Dim nameCheck As Range
    On Error GoTo rangeError
    Set nameCheck = Range(refName)
    ActiveWorkbook.Names(refName).Delete
    
rangeError:
    Range(startCol & row).Name = refName
    
End Function
    
Sub getTotals()
    Dim nm As Name
    For Each nm In ThisWorkbook.Names
        Dim n As String
        n = Split(nm, "!")(0)
        If Right(n, Len(n) - 1) <> "Investment" Then
            Range(nm).Offset(0, -1).Value = ""
        Else
            Range(nm).Offset(-2, -3).Clear
            Range(nm).Offset(-2, -2).Clear
            Range(nm).Offset(-2, -0).Clear
            
            Range(nm).Offset(2, -3).Clear
            Range(nm).Offset(2, -2).Clear
            Range(nm).Offset(2, 0).Clear
            
            Range(nm).Offset(0, -3).Clear
            
        End If
        Range(nm).Clear
        nm.Delete
    Next nm
    
    calSum "Original Assets", "B", 7, "Total_Assets", "Total Assets"
    calSum "Cap. Rec.", "D", 5, "Total_Receipts", "Total Receipts"
    calSum "Cap. Disb.", "D", 5, "Total_Disbursements", "Total Disbursements"
    calSum "Rev. Rec.", "D", 5, "Total_Revenue_Receipts", "Total Revenue Receipts"
    calSum "Rev. Disb.", "D", 5, "Total_Revenue_Disbursements", "Total Revenue Disbursements"
    calSum "Unrealized", "B", 3, "Unrealizedoriginalassets", "Total"
    calSum "Invest on hand", "B", 3, "Invest_On_Hand", "Total"
    calInvestment "Investment", "D", "F", 5

End Sub Sub clearTotals()
 Dim nm As Name
    For Each nm In ThisWorkbook.Names
        Dim n As String
        n = Split(nm, "!")(0)
        If Right(n, Len(n) - 1) <> "Investment" Then
            Range(nm).Offset(0, -1).Value = ""
        Else
            Range(nm).Offset(-2, -3).Clear
            Range(nm).Offset(-2, -2).Clear
            Range(nm).Offset(-2, -0).Clear
            
            Range(nm).Offset(2, -3).Clear
            Range(nm).Offset(2, -2).Clear
            Range(nm).Offset(2, 0).Clear
            
            Range(nm).Offset(0, -3).Clear
            
        End If
        Range(nm).Clear
        nm.Delete
    Next nm
End Sub
99130i.png
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
This will sum column b on the sheet name you specify and put that sum in the cell below the last entry in column B.

Code:
Sub SumColumnB()
    Dim r As Range
    With Sheets("YourSheetName")
        Set r = .Range("B" & .Rows.Count).End(xlUp).Offset(1)
        r.Value = WorksheetFunction.Sum(.Range("B:B"))
    End With
End Sub
 
Upvote 0
Thank you so much! this worked perfectly!
This will sum column b on the sheet name you specify and put that sum in the cell below the last entry in column B.

Code:
Sub SumColumnB()
    Dim r As Range
    With Sheets("YourSheetName")
        Set r = .Range("B" & .Rows.Count).End(xlUp).Offset(1)
        r.Value = WorksheetFunction.Sum(.Range("B:B"))
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,767
Messages
6,174,388
Members
452,561
Latest member
amir5104

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