Calculation based on last cell in a row +1

Gingertrees

Well-known Member
Joined
Sep 21, 2009
Messages
697
column A has dates, column B has values.
so, e.g.
A B
8/1 200
8/2 123
8/3 64
8/4 211
8/5 187
8/6 46

etc
It's long. I want excel to find the last row, and show a sum of the values one row above that row.

So if 8/6 is the last row of used data, I want a sum of the B values from 8/1 - 8/5 (omitting bottom row from sum).

I can find the last row with
Lastdata1 = Range("B65536").End(xlUp).Row

But I don't know how to get that into something like
Sum (B2:2ndtolastrow).

And this should be like a workbook_open or worksheet_change or something, as the numbers in the bottom row change constantly.
(Banging head repeatedly as I try and fail to write the correct syntax).
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Lastdata1 = Range("B65536").End(xlUp).Row-1
would give you the row above the last row
 
Upvote 0
Try like this

Code:
Sub Test()
Dim LR As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
Range("C2").Formula = "=SUM(B2:B" & LR - 1 & ")"
End Sub
 
Upvote 0
Thanks, but now I have a new problem.
I placed this in the worksheet module:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim LR As Long
    On Error GoTo 0
    LR = Range("B" & Rows.Count).End(xlUp).Row
    Range("H1").Formula = "=SUM(B2:B" & LR - 1 & ")"
End Sub
While it does the correct calculation, it causes a fatal error that throws the "Excel has encountered an error and needs to close" message.

???
 
Upvote 0
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim LR As Long
    Application.EnableEvents = False
    LR = Range("B" & Rows.Count).End(xlUp).Row
    Range("H1").Formula = "=SUM(B2:B" & LR - 1 & ")"
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,283
Members
452,902
Latest member
Knuddeluff

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