Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SUMPRODUCT in Code 4

Status
Not open for further replies.

SkipVought

Programmer
Dec 4, 2001
47,492
US
What am I missing?

Take a simple table like
[tt]
Name Amt
Skip 1
Mary 2
Fred 3
Skip 4
Fred 5
Mary 6
Skip 7
[/tt]
Code:
    Dim n
    n = Application.SumProduct([b](Range("Name") = "Skip")[/b] * (Range("Amt")))
I get Type Mismatch.

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
Just a guess:
n = Application.SumProduct((Range("Name") = "Skip"), (Range("Amt")))
Or:
n = Application.Sum((Range("Name") = "Skip") * (Range("Amt")))

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 


PHV, Thanks for the try -- but no dice! :-(

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
I think I see what you're driving at. Both of these work directly in a worksheet:
[tt]
=SUMPRODUCT((Name="Skip")*Amt)
=SUMIF(Name,"Skip",Amt)
[/tt]
but only this one works in VBA:
Code:
Sub Test()
Dim n
  n = WorksheetFunction.SumIf([Name], "Skip", [Amt])
MsgBox n
End Sub
It seems that this one should work, but it doesn't (Excel 97 SR-2):
Code:
Sub Test2()
Dim n
  n = WorksheetFunction.SumProduct(([Name] = "Skip") * [Amt])
MsgBox n
End Sub
I never really liked SUMPRODUCT for this sort of thing anyway. [lol]

 
In fact the problem is here:
(Range("Name") = "Skip")

I guess it should be a FormulaArray ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 


Thanks Z,

I am attempting to replace several complex formulas with UDF's. This one is the least complex, so i might just forget this entire thing if it looks like more effort than its worth.

So I want to replace this...
[tt]
=IF(SUMPRODUCT((PART_ID=$A3)*(MDCN=$L3)*(PCT<=EndCalDate)*(QTY))>B3,B3,SUMPRODUCT((PART_ID=$A3)*(MDCN=$L3)*(PCT<=EndCalDate)*(QTY)))
[/tt]
with something like...
Code:
Function EDQ(sPN As String, sMDCN As String, iRQ As Integer) As Integer
    Dim n
    With wsSprOrds
        n = Application.SumProduct( _
            (.Range("PART_ID") = sPN) * _
            (.Range("MDCN") = sMDCN) * _
            (.Range("PCT") <= Range("EndCalDate")) * _
            (.Range("QTY")))
    End With
    If n > iRQ Then
        EDQ = iRQ
    Else
        EDQ = n
    End If
End Function

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 

Pity. However, a "proper" UDF to replace that would require more parameters. Otherwise it would be a kludge with direct references to parts of the worksheet that aren't explicitly identified in the parameter list.

You could still simplify the formulas, however. You could use a formula that looks like this:
[tt]
=MIN(DSUM(database,4,criteria),B3)
[/tt]
by simply setting up the critera range thus:
[tt]
PART_ID MDCN
=$A$3 =$L$3 =PCT<EndCalDate
[/tt]
(Leave the cell above "=PCT<EndCalDate" blank.)

In fact, you could at least get a little simplification by replacing the IF structure with MIN:
[tt]
=MIN(SUMPRODUCT((PART_ID=$A3)*(MDCN=$L3)*(PCT<=EndCalDate)*(QTY)),B3)
[/tt]
 

Z,

I accept your castigation regarding a PROPER UDF!

I'll check out your suggestions.

Thanx!

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
Pretty certain it throws an error as the way we use SumProduct is not actually the way it was designed to be used.

If you notice, the arguments for Sumproduct are:

=sumproduct(array1,array2...etc etc

in formulae, we can enter a range and have it expressed as an array - I have a feeling you cannot implicitly do the same in code - we are also corrupting the actual syntax of the formula as well so I am not that surprised that it doesn't like it in code

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
hmmm... Probably missing something here, but wouldn't

=SUMIF(A1:A7,"Skip",B1:B7)
Do the job?
 
Skip - from another source (didn't work this out for myself) - the following works:
Code:
Sub test()
x = ActiveSheet.[b]Evaluate("[/b]SumProduct((srNames=""Skip"")*(srAmt))[b]")[/b]
End Sub

where 'srNames' is a named range, as is 'srAmt'

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 

Geoff,

Thanks!

U da man!

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
Hey Geoff, well found ... I'll be noting that for future use. Have a ---> * [smile]


Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 


Actually, guys, my EXAMPLE was much simpler than real-life..

As my code indicates, I have FOUR ranges, THREE of which have criteria. So a SUMIF, for instance, would not suffice.

Thanx to all.

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
Cheers guys but you can thank a chap called Bob Phillips on the Excel-L list for this little gem - funnily enough (happens a lot actually), someone had posted a very similar question to Skip's so I just checked the answer out and it worked.....

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
aaaah.. You didn't mention that in the OP ;) thought it seemed too simple an answer!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top