LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Using a UDF within SUMPRODUCT

I am trying to use a UDF within a SUMPRODUCT formula, but am having a problem
getting the UDF to correctly return an array. Simplified data in A1 thru B24:

23 1
14 2
16 2
9 1
21 2
10 2
20 1
17 3
13 1
15 1
22 3
8 3
2 1
19 1
3 1
1 2
4 2
11 3
6 2
18 2
5 3
7 2
12 1
24 3

I need to count the number of rows in which the value in column A is prime
and the value in column B is 3. The values in column A are always 25 or
less. My sad attempt at a UDF is:

Function prime(r As Range) As Variant
pm = Array(1, 2, 3, 5, 7, 11, 13, 17, 19, 23)
Dim v() As Variant
ReDim v(1 To r.Count)
j = 1
For Each rr In r
x = rr.Value
v(j) = 0
For i = 0 To 9
If x = pm(i) Then
v(j) = 1
End If
Next
j = j + 1
Next
prime = v
End Function

The function should return a 1 if the argument is prime, otherwise 0.

The function works for single items like:
=prime(A1)
The function also works within SUMPRODUCT like:
=SUMPRODUCT(--(prime(A1:A24)=1))

The function, however, returns #VALUE!
for:
=SUMPRODUCT(--(prime(A1:A24)=1),--(B1:B24=3))

(I usually get this if the sumproduct inputs are of different lengths)

This is not urgent since I can use a helper column until I can get
sumproduct to work.

Thanks in advance for any help.
--
Gary''s Student - gsnu200797
 
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
Sumproduct with Condition OR Sumproduct with ADDRESS function - HE gholly Excel Discussion (Misc queries) 2 September 28th 09 05:07 PM
SumProduct ColleenK Excel Worksheet Functions 7 March 12th 09 03:47 PM
Conditional SUMPRODUCT or SUMPRODUCT with Filters Ted M H Excel Worksheet Functions 4 August 14th 08 07:50 PM
sumproduct? sumif(sumproduct)? David Excel Worksheet Functions 3 July 13th 07 07:06 PM
help with sumproduct nastech Excel Discussion (Misc queries) 4 June 24th 06 08:54 PM


All times are GMT +1. The time now is 02:56 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"