Autosum until previous cell with sum

vaffer

New Member
Joined
May 19, 2014
Messages
2
Hi everyone !

I have a list of items that is seperated into sections, simply by blank cells.
In every section there are items in column B and corresponding values in colulmn C.
What I want is an automatic sum of every section/day, this is exactly what the AutoSum button does, but it should happen automatically when "total" is written in the A/last row of a section.

ABC
1day 1
2item 1150
3item 3320
4item 6220
5total690
6
7day 2
8item 2180
9item 4250
10item 5100
11total530

<tbody>
</tbody>

So when I type total in A11 it should sum the values of day 2 or values until the last "total" (c6:c10), and so on.
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
try this code. Paste it to the Sheet Module where you want it to work.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
Dim lStart As Long, lEnd As Long
Dim irow1 As Integer, irow2 As Integer

If Target.Count > 1 Then Exit Sub

If Not Target.Column = 1 Then Exit Sub

If LCase(Target.Value) Like "total" Then
    lEnd = Target.Row - 1
    If Cells(lEnd - 1, 2) = Empty Then
        lStart = lEnd
    Else
        lStart = Cells(lEnd, 2).End(xlUp).Row
    End If
End If

irow1 = -(Target.Row - lStart)
irow2 = -1

Cells(Target.Row, 3).FormulaR1C1 = "=SUM(R[" & irow1 & "]C:R[" & irow2 & "]C)"

End Sub
 
Upvote 0
Helo skorpionkz !

Thank you, but it is not perfect.
This was only an example. I hoped to modify the code to my needs but I can't, and I have 3 problems.

1, AutoSum happens every time I type someting in column A. For example when I type "day 3" in A13, it sums A12:A1048576 with the result 0.

2, The word "total" is put in column A with an IF function, but this way the code isn't working, it sums A(ROW"total")-1:A1048576 with the result 0.

3, The autosum should happen in column G
 
Upvote 0
corrected code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
Dim lStart As Long, lEnd As Long
Dim irow1 As Integer, irow2 As Integer

If Target.Count > 1 Then Exit Sub
If Not Target.Column = 1 Or _
Not LCase(Target.Value) Like "total" Then Exit Sub
    
lEnd = Target.Row - 1
If Cells(lEnd - 1, 2) = Empty Then
    lStart = lEnd
Else
    lStart = Cells(lEnd, 2).End(xlUp).Row
End If

irow1 = -(Target.Row - lStart)
irow2 = -1

Cells(Target.Row, 7).FormulaR1C1 = "=SUM(R[" & irow1 & "]C:R[" & irow2 & "]C)"

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,016
Messages
6,122,700
Members
449,092
Latest member
snoom82

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