Sum last X entries in a row

Marycrawford

New Member
Joined
Nov 28, 2017
Messages
5
[FONT=&quot]Dear All,[/FONT]
[FONT=&quot]I’m pretty new to Excel and appreciate any guidance regarding the following issue that I encounter;

In the following, I have a row of numbers; What I want to be in the second row is the sum of the last “X” number of elements; Meaning that for X=4, as long as the number of values in the first row is less than X=4, the second row will show the sum of the non empty cells in the first row and once the X+1 cell (5 for this example) gets filled with a value, the very first entry in the first row is subtracted from the sum; for the next coming value in cell X+2 of the first row, the second entry in the first row is subtracted from the sum and so on.[/FONT]

[FONT=&quot]Hence, we have a moving average of the sum of last X number of elements shown in the second row and in case the number of elements in the first row is less than X it doesn’t give an error but instead sum of the available entries.

[/FONT]

12345678910
1=13=1+26=3+310=6+414=10+5-118=14+6-222=18+7-326=22+8-430=26+9-534=30+10-6

<colgroup><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>


Thanks in advance for any guidance.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi, welcome to the board!

Something like this maybe, where the 4 is your X.

=SUM(A1:INDEX($A1:$Z1,MAX(1,COLUMN(A1)-COLUMN($A$1)+1-(4-1))))



Excel 2013/2016
ABCDEFGHIJ
112345678910
213610141822263034
Sheet1
Cell Formulas
RangeFormula
A2=SUM(A1:INDEX($A1:$Z1,MAX(1,COLUMN(A1)-COLUMN($A$1)+1-(4-1))))
 
Upvote 0
Hi and many thanks for your answer; however, as it's clear from below when I fill in just first and second entries in the first row, besides the correct entries in the second row (A2 and B2), the irrelevant entries are also filled with some redundant data: C2=3, D2=3, E2=2.

12
13332000000000000

<colgroup><col span="2"><col><col><col><col><col><col><col><col><col><col span="6"></colgroup><tbody>
</tbody>

Could you please help me how to get rid of this effect?
Many thanks beforehand for your help.
 
Upvote 0
GREAAAAT! many many thanks for your quick awesome response.

Now, can I kindly ask another question that I guess is rather easy to an expert like you :)
What I want to do is to have a function is a cell that will get input in the same cell; What I want exactly, is to add X% to the input value that a user inserts into a cell; So, I want a function saying that, once a value inserted here, add X% to it and put the new value in the same cell.
I wonder would that be possible?
Many thanks again.
 
Upvote 0
So, I want a function saying that, once a value inserted here, add X% to it and put the new value in the same cell.

You would need to use VBA for that, probably a "change event" assuming that the value would be entered manually.
 
Upvote 0
May I ask what is a VBA?

Hi, it's a programming language for MS office applications that enables all sorts of automation.

Yes, the value would be entered manually by the user

Where is the value entered:
- A single cell (if so which cell)?
- Or a range of cells? (if so, which column, row or range of cells)?

Where does the x% come from:
- is it fixed (if so what is it)?
- or stored somewhere (if so where)?
- or logically derived (if so how)?
 
Upvote 0
OK - you can try this event code.

To use:

1. Right click the sheet tab where you want this to happen and choose "View Code".
2. Copy and paste the code below to the top right hand side window.
3. Press ALT+Q to close the code editor
4. Save your file as a macro enabled workbook (*.XLSM)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, c As Range
Set r = Intersect(Target, Range("A10:Z10"))
If Not r Is Nothing Then
  Application.EnableEvents = False
  For Each c In r
    If IsNumeric(c.Value) And Len(c.Value) Then
      c.Value = c.Value + (c.Value * Range("A1").Value)
    End If
  Next c
  Application.EnableEvents = True
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,500
Messages
6,131,016
Members
449,615
Latest member
Nic0la

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