Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 181
Default XL2002 SUM with a twist...

Hi All,

Is there a way to SUM a variable amount of values 0 where the number of
values to SUM is in another cell? -- let me try and explain a bit better.

Cell A1 has a value of 3 (this is how many values I need to SUM from the data)

Cells A2:F2 contains the data series - BUT
Cells B2 & D2 contain a zero

So, I need to SUM A2, C2, E2 (first 3 values greater than 0)

Also, if there are only 2 values 0 in the data series the SUM returns the
total those 2 values (without error) and ignores the fact it's looking for
3...

Hope that makes sense(?)

Look forward to your response.
Regards
Trevor Williams
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,722
Default XL2002 SUM with a twist...

You can use this UDF:

'=========
Public Function SpecialSum(r As Range, c As Double) As Double
Value = 0

For Each cell In r
If c 0 Then
If cell.Value 0 Then
'Add to total
Value = Value + cell.Value
'Deduct from count of cells to look for
c = c - 1
End If
End If
Next cell

SpecialSum = Value
End Function
'=========

The formula in your workbook would then be:
=SpecialSum(A2:F2,A1)


To install a UDF:
Press Alt+F11 to bring up the VBE (Visual Basic Editor). Goto Insert -
Module. Paste the coding in. Close the VBE.

--
Best Regards,

Luke M
*Remember to click "yes" if this post helped you!*


"Trevor Williams" wrote:

Hi All,

Is there a way to SUM a variable amount of values 0 where the number of
values to SUM is in another cell? -- let me try and explain a bit better.

Cell A1 has a value of 3 (this is how many values I need to SUM from the data)

Cells A2:F2 contains the data series - BUT
Cells B2 & D2 contain a zero

So, I need to SUM A2, C2, E2 (first 3 values greater than 0)

Also, if there are only 2 values 0 in the data series the SUM returns the
total those 2 values (without error) and ignores the fact it's looking for
3...

Hope that makes sense(?)

Look forward to your response.
Regards
Trevor Williams

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 15,768
Default XL2002 SUM with a twist...

Try this array formula** :

=SUM(A2:INDEX(A2:F2,SMALL(IF(A2:F20,COLUMN(A2:F2) ),MIN(A1,COUNTIF(A2:F2,"0")))))

** array formulas need to be entered using the key combination of
CTRL,SHIFT,ENTER (not just ENTER). Hold down both the CTRL key and the SHIFT
key then hit ENTER.

If A1 is an empty cell the formula will calculate the entire range.

If there are no values 0 the formula will return the error #NUM!.

--
Biff
Microsoft Excel MVP


"Trevor Williams" wrote in
message ...
Hi All,

Is there a way to SUM a variable amount of values 0 where the number of
values to SUM is in another cell? -- let me try and explain a bit better.

Cell A1 has a value of 3 (this is how many values I need to SUM from the
data)

Cells A2:F2 contains the data series - BUT
Cells B2 & D2 contain a zero

So, I need to SUM A2, C2, E2 (first 3 values greater than 0)

Also, if there are only 2 values 0 in the data series the SUM returns
the
total those 2 values (without error) and ignores the fact it's looking for
3...

Hope that makes sense(?)

Look forward to your response.
Regards
Trevor Williams



  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 181
Default XL2002 SUM with a twist...

Hi Luke - thanks for your response.
I
ve actually developed a bit of code to do the calcs, but it takes a fair bit
of time to cycle through 000's of lines of data.

I'll give your Function a go and report back -- hopefully it's a lot quicker!

Trevor

"Luke M" wrote:

You can use this UDF:

'=========
Public Function SpecialSum(r As Range, c As Double) As Double
Value = 0

For Each cell In r
If c 0 Then
If cell.Value 0 Then
'Add to total
Value = Value + cell.Value
'Deduct from count of cells to look for
c = c - 1
End If
End If
Next cell

SpecialSum = Value
End Function
'=========

The formula in your workbook would then be:
=SpecialSum(A2:F2,A1)


To install a UDF:
Press Alt+F11 to bring up the VBE (Visual Basic Editor). Goto Insert -
Module. Paste the coding in. Close the VBE.

--
Best Regards,

Luke M
*Remember to click "yes" if this post helped you!*


"Trevor Williams" wrote:

Hi All,

Is there a way to SUM a variable amount of values 0 where the number of
values to SUM is in another cell? -- let me try and explain a bit better.

Cell A1 has a value of 3 (this is how many values I need to SUM from the data)

Cells A2:F2 contains the data series - BUT
Cells B2 & D2 contain a zero

So, I need to SUM A2, C2, E2 (first 3 values greater than 0)

Also, if there are only 2 values 0 in the data series the SUM returns the
total those 2 values (without error) and ignores the fact it's looking for
3...

Hope that makes sense(?)

Look forward to your response.
Regards
Trevor Williams

  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 181
Default XL2002 SUM with a twist...

Hi Luke -- works a treat. Thak you very much.
Trevor

"Luke M" wrote:

You can use this UDF:

'=========
Public Function SpecialSum(r As Range, c As Double) As Double
Value = 0

For Each cell In r
If c 0 Then
If cell.Value 0 Then
'Add to total
Value = Value + cell.Value
'Deduct from count of cells to look for
c = c - 1
End If
End If
Next cell

SpecialSum = Value
End Function
'=========

The formula in your workbook would then be:
=SpecialSum(A2:F2,A1)


To install a UDF:
Press Alt+F11 to bring up the VBE (Visual Basic Editor). Goto Insert -
Module. Paste the coding in. Close the VBE.

--
Best Regards,

Luke M
*Remember to click "yes" if this post helped you!*


"Trevor Williams" wrote:

Hi All,

Is there a way to SUM a variable amount of values 0 where the number of
values to SUM is in another cell? -- let me try and explain a bit better.

Cell A1 has a value of 3 (this is how many values I need to SUM from the data)

Cells A2:F2 contains the data series - BUT
Cells B2 & D2 contain a zero

So, I need to SUM A2, C2, E2 (first 3 values greater than 0)

Also, if there are only 2 values 0 in the data series the SUM returns the
total those 2 values (without error) and ignores the fact it's looking for
3...

Hope that makes sense(?)

Look forward to your response.
Regards
Trevor Williams

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
XL2002 - VLOOKUP with variable Sheet Name Trevor Williams Excel Worksheet Functions 3 July 29th 08 05:33 PM
Semi-hang in XL2002 Charlie Excel Discussion (Misc queries) 0 May 6th 06 04:53 PM
HOW TO COPY XL2000 (XP) MACROS TO XL2002 (XP) will A Excel Discussion (Misc queries) 0 January 16th 06 06:48 PM
Using min and max function XL2002 Extremely Aggravated Excel Worksheet Functions 1 September 23rd 05 06:29 PM
Can not open .wb1 file with XL2000 and XL2002 Gaurav Excel Discussion (Misc queries) 1 March 8th 05 12:21 PM


All times are GMT +1. The time now is 05:59 AM.

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

About Us

"It's about Microsoft Excel"