need help on a function

mark gardner

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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Oaktree

MrExcel MVP
Joined
Jun 20, 2002
Messages
8,010
Office Version
  1. 365
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.
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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
 

Forum statistics

Threads
1,137,061
Messages
5,679,388
Members
419,825
Latest member
MegastarMagus

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
Top