Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ev Ev is offline
external usenet poster
 
Posts: 5
Default Sum the greatest run of negative numbers

I have a list of returns like that listed below, I want to find out the
greatest continuous run of negative returns,
(i.e. -24-3 =-27, and is greater than -2-4-3-1-1-1=-12) so it would return a
result of -27
6
7
2
0
-2
-4
-3
-1
-1
-1
7
-24
-3
2
4

A function would be the most preferable solution


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Sum the greatest run of negative numbers

Ev,
How about this ?
Function CalcMaxNeg(argRange As Range) As Long
Dim Cell As Range
Dim RunSum As Long
Dim MaxNeg As Long

For Each Cell In Range("rngData").Cells
If Cell.Value < 0 Then
RunSum = RunSum + Cell.Value
Else
If RunSum < MaxNeg Then MaxNeg = RunSum
RunSum = 0
End If
Next

CalcMaxNeg = MaxNeg
End Function

NickHK

"Ev" wrote in message
...
I have a list of returns like that listed below, I want to find out the
greatest continuous run of negative returns,
(i.e. -24-3 =-27, and is greater than -2-4-3-1-1-1=-12) so it would return

a
result of -27
6
7
2
0
-2
-4
-3
-1
-1
-1
7
-24
-3
2
4

A function would be the most preferable solution




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default Sum the greatest run of negative numbers

Function negmax(xRng As Range) As Long
Dim xCell As Range
Dim xSum As Long
xSum = 0: negmax = 0
For Each xCell In xRng
If xCell.Value < 0 Then
xSum = xSum + xCell.Value
Else
xSum = 0
End If
Debug.Print xSum, negmax
If xSum < negmax Then negmax = xSum
Next
End Function

You call the function using

negmax(Range("A1:A14"))


--
Cheers
Nigel



"Ev" wrote in message
...
I have a list of returns like that listed below, I want to find out the
greatest continuous run of negative returns,
(i.e. -24-3 =-27, and is greater than -2-4-3-1-1-1=-12) so it would return
a result of -27
6
7
2
0
-2
-4
-3
-1
-1
-1
7
-24
-3
2
4

A function would be the most preferable solution



  #4   Report Post  
Posted to microsoft.public.excel.programming
Ev Ev is offline
external usenet poster
 
Posts: 5
Default Sum the greatest run of negative numbers

Thanks Nick,

But I can't get it to work,
I've inserted the code in a module and closed and re-opened excel but the
formula CalcMaxNeg() doesn't work and returns #Value


"NickHK" wrote in message
...
Ev,
How about this ?
Function CalcMaxNeg(argRange As Range) As Long
Dim Cell As Range
Dim RunSum As Long
Dim MaxNeg As Long

For Each Cell In Range("rngData").Cells
If Cell.Value < 0 Then
RunSum = RunSum + Cell.Value
Else
If RunSum < MaxNeg Then MaxNeg = RunSum
RunSum = 0
End If
Next

CalcMaxNeg = MaxNeg
End Function

NickHK

"Ev" wrote in message
...
I have a list of returns like that listed below, I want to find out the
greatest continuous run of negative returns,
(i.e. -24-3 =-27, and is greater than -2-4-3-1-1-1=-12) so it would
return

a
result of -27
6
7
2
0
-2
-4
-3
-1
-1
-1
7
-24
-3
2
4

A function would be the most preferable solution






  #5   Report Post  
Posted to microsoft.public.excel.programming
Ev Ev is offline
external usenet poster
 
Posts: 5
Default Sum the greatest run of negative numbers

Thanks All works great
"Ev" wrote in message
...
I have a list of returns like that listed below, I want to find out the
greatest continuous run of negative returns,
(i.e. -24-3 =-27, and is greater than -2-4-3-1-1-1=-12) so it would return
a result of -27
6
7
2
0
-2
-4
-3
-1
-1
-1
7
-24
-3
2
4

A function would be the most preferable solution





  #6   Report Post  
Posted to microsoft.public.excel.programming
Ev Ev is offline
external usenet poster
 
Posts: 5
Default Sum the greatest run of negative numbers

Hi Guys,

The initial function works but only with whole numbers, it doesn't seem to
work with % and also seems to round. Is there a way to change it to work
around this?

"Ev" wrote in message
...
I have a list of returns like that listed below, I want to find out the
greatest continuous run of negative returns,
(i.e. -24-3 =-27, and is greater than -2-4-3-1-1-1=-12) so it would return
a result of -27
6
7
2
0
-2
-4
-3
-1
-1
-1
7
-24
-3
2
4

A function would be the most preferable solution



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Sum the greatest run of negative numbers

Ev,
Just change all Longs to Singles. e.g.:

Function CalcMaxNeg(argRange As Range) As Long
Dim Cell As Range
Dim RunSum As Single
Dim MaxNeg As Single

.........etc

NickHK

"Ev" wrote in message
...
Hi Guys,

The initial function works but only with whole numbers, it doesn't seem to
work with % and also seems to round. Is there a way to change it to work
around this?

"Ev" wrote in message
...
I have a list of returns like that listed below, I want to find out the
greatest continuous run of negative returns,
(i.e. -24-3 =-27, and is greater than -2-4-3-1-1-1=-12) so it would

return
a result of -27
6
7
2
0
-2
-4
-3
-1
-1
-1
7
-24
-3
2
4

A function would be the most preferable solution





  #8   Report Post  
Posted to microsoft.public.excel.programming
Ev Ev is offline
external usenet poster
 
Posts: 5
Default Sum the greatest run of negative numbers

Thanks, sorry for stupid question.
"NickHK" wrote in message
...
Ev,
Just change all Longs to Singles. e.g.:

Function CalcMaxNeg(argRange As Range) As Long
Dim Cell As Range
Dim RunSum As Single
Dim MaxNeg As Single

........etc

NickHK

"Ev" wrote in message
...
Hi Guys,

The initial function works but only with whole numbers, it doesn't seem
to
work with % and also seems to round. Is there a way to change it to work
around this?

"Ev" wrote in message
...
I have a list of returns like that listed below, I want to find out the
greatest continuous run of negative returns,
(i.e. -24-3 =-27, and is greater than -2-4-3-1-1-1=-12) so it would

return
a result of -27
6
7
2
0
-2
-4
-3
-1
-1
-1
7
-24
-3
2
4

A function would be the most preferable solution







Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel, change column of negative numbers to positive numbers? Nita New Users to Excel 3 November 27th 07 04:54 AM
Excel 2002 : Convert Positive Numbers to Negative Numbers ? Mr. Low Excel Discussion (Misc queries) 2 November 6th 06 03:30 PM
Set negative numbers to zero. Do not calculate with negative valu Excel Headache Excel Discussion (Misc queries) 4 September 14th 06 08:56 PM
change 2000 cells (negative numbers) into positive numbers lisbern Excel Worksheet Functions 2 August 16th 06 05:54 PM
In Excel, how to sum top 10 greatest values in a row of numbers? Michele P Excel Worksheet Functions 4 January 19th 05 04:08 PM


All times are GMT +1. The time now is 07:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"