'Formula to indicate how many cells to add in a sum

Pls assist with a formula if you can. Let's say a have a line of numbers - A1 to A10. In B1 to B10 I have the numbers 1 to 10. I want to write a formula which will calculate the sum of the number of cells indicated. Let's say I input '5' - the formula must take A1 + A2 + A3 + A4 + A5 = result.

Thanks



Solution 1:[1]

try:

=SUM(INDIRECT("A1:A"&D2))

enter image description here

Solution 2:[2]

=SUM(A1:INDEX(A:A,C1)) Where C1 is your input value (used as the row number for the index).

This does the same as the formula suggested by player0, but in Excel INDIRECT is volatile, which will recalculate if any change is made anywhere in the workbook, which can slow down Excel.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 player0
Solution 2 P.b