PC Review


Reply
Thread Tools Rate Thread

Application.Worksheetfunction.Sumproduct

 
 
Tetsuya Oguma
Guest
Posts: n/a
 
      16th Jul 2008
Hi all,

I have got this groovy formula below as an Excel formula and would like to
make it usable as VBA:

=SUMPRODUCT(--(B3:B16-B2:B15>=0),B3:B16-B2:B15)

I realised this "-" in the formula means "multiply by -1".

I tried:

Dim v As Variant
v = Application.WorksheetFunction.SumProduct(--(Range("B3:b16") -
Range("b2:b15") >= 0), Range("B3:b16") - Range("b2:b15"))

But I get Type Mismatch error...

Can anyone help?

Thanks in advance,
Tetsuya

 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      16th Jul 2008
v = Activesheet.Evaluate("SUMPRODUCT(--(B3:B16-B2:B15>=0),B3:B16-B2:B15)")


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Tetsuya Oguma" <(E-Mail Removed)> wrote in message
news:67597D29-04E8-4DFE-9785-(E-Mail Removed)...
> Hi all,
>
> I have got this groovy formula below as an Excel formula and would like to
> make it usable as VBA:
>
> =SUMPRODUCT(--(B3:B16-B2:B15>=0),B3:B16-B2:B15)
>
> I realised this "-" in the formula means "multiply by -1".
>
> I tried:
>
> Dim v As Variant
> v = Application.WorksheetFunction.SumProduct(--(Range("B3:b16") -
> Range("b2:b15") >= 0), Range("B3:b16") - Range("b2:b15"))
>
> But I get Type Mismatch error...
>
> Can anyone help?
>
> Thanks in advance,
> Tetsuya
>



 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      16th Jul 2008
Will this do?

Total = 0
for each cell in Range("B3:B16")
if cell >= cell.offset(-1,0) then
total = total + (cell - cell.offset(-1,0))
end if
next cell

The "--" really converts the TRUE's to 1 and the False's to 0. so instead
of getting an array of

{true, false, false, True,...)

You get

{1,0,0,1,...}

Sumproduct doesn't know how to multiply {true * 1}. the -- changes this to
{1 * 1}

"Tetsuya Oguma" wrote:

> Hi all,
>
> I have got this groovy formula below as an Excel formula and would like to
> make it usable as VBA:
>
> =SUMPRODUCT(--(B3:B16-B2:B15>=0),B3:B16-B2:B15)
>
> I realised this "-" in the formula means "multiply by -1".
>
> I tried:
>
> Dim v As Variant
> v = Application.WorksheetFunction.SumProduct(--(Range("B3:b16") -
> Range("b2:b15") >= 0), Range("B3:b16") - Range("b2:b15"))
>
> But I get Type Mismatch error...
>
> Can anyone help?
>
> Thanks in advance,
> Tetsuya
>

 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      16th Jul 2008


"Joel" <(E-Mail Removed)> wrote in message
news:59798771-5907-4BE8-BE4C-(E-Mail Removed)...

>
> Sumproduct doesn't know how to multiply {true * 1}. the -- changes this to
> {1 * 1}


=SUMPRODUCT(({TRUE,TRUE})*({1,1}))

=2


 
Reply With Quote
 
Tom Ogilvy
Guest
Posts: n/a
 
      16th Jul 2008
Just to add to the information

You can use sumproduct for its original intended purpose of multiplying two
or more arrays element by element and summing the results using
application.Sumproduct.

However your "groovy" formula is using the special ability of sumproduct in
a worksheet to evaluate it arguments as array formulas. This ability is not
supported in VBA. the Evaluate function in VBA can interpret a worksheet
formula supplied as an argument as if it were in a virtual cell. thus Bob
has suggested that use which does support the special abilities of sumproduct
to act as an array formula.

--
Regards,
Tom Ogilvy


"Tetsuya Oguma" wrote:

> Hi all,
>
> I have got this groovy formula below as an Excel formula and would like to
> make it usable as VBA:
>
> =SUMPRODUCT(--(B3:B16-B2:B15>=0),B3:B16-B2:B15)
>
> I realised this "-" in the formula means "multiply by -1".
>
> I tried:
>
> Dim v As Variant
> v = Application.WorksheetFunction.SumProduct(--(Range("B3:b16") -
> Range("b2:b15") >= 0), Range("B3:b16") - Range("b2:b15"))
>
> But I get Type Mismatch error...
>
> Can anyone help?
>
> Thanks in advance,
> Tetsuya
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
application.worksheetfunction JP Ronse Microsoft Excel Programming 8 23rd Aug 2009 04:26 PM
WorksheetFunction.Sumproduct(syntax... =?Utf-8?B?RGF2aWQ=?= Microsoft Excel Programming 10 20th Aug 2009 07:26 PM
WorksheetFunction.sumproduct limits? =?Utf-8?B?RGF2aWQ=?= Microsoft Excel Programming 1 3rd Nov 2004 09:27 PM
Application.worksheetfunction Terry V Microsoft Excel Programming 7 12th Oct 2004 05:48 AM
Using Application.WorksheetFunction.Ln(...) in VBA doco Microsoft Excel Programming 4 25th Aug 2003 01:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:15 PM.