need help on a function

mark gardner

Board Regular
Joined
Sep 6, 2006
Messages
58
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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

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