need help on a function

mark gardner

Board Regular
I need a function or formula that will read a number in a cell and then give me the sum of all numers between 1 and that number. Can anyone help me?

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If A1 contains your number try:

=SUMPRODUCT(ROW(INDIRECT("1:"&A1)))

Welcome to the board.

If A1 stores your number, try:

=SUM(ROW(INDIRECT("1:"&A1))), which is an array formula and must be confirmed with CTRL+SHIFT+ENTER (doing so will result in Excel putting { }'s around your formula in the formula bar).

Of course, this limits your input to a maximum of 65536. If that doesn't suit your needs, you'll likely need to use VBA.

Hi

A possible VBA solution utilising a UDF could be:

Code:
Function SumOneToN(LNumber As Long) As Double
Dim n As Integer, temp As Double
n = 1
If LNumber < 0 Then
n = -1
LNumber = Abs(LNumber)
End If
If LNumber <= 1 Then SumOneToN = LNumber * n: Exit Function
Do While LNumber > 0
temp = temp + LNumber
LNumber = LNumber - 1
Loop
SumOneToN = temp * n
End Function

Which will also work with negative numbers if that interests you.

Best regards

Richard

Replies
4
Views
137
Replies
9
Views
443
Replies
9
Views
127
Replies
4
Views
221
Replies
7
Views
117

1,221,206
Messages
6,158,520
Members
451,497
Latest member
something68

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.

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

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