Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / April 2008

Tip: Looking for answers? Try searching our database.

SumProduct as function in VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
olssonj - 18 Apr 2008 12:07 GMT
Hi all, I am new to macros and VBA. Have a question concerning SumProduct.

I have a worksheet with 20 something active columns. In a for-loop I want to
ad dates from column G that depends on values from two other columns B and F.
I am trying to use SumProduct in VBA. See description and my code below:

Column B        ColumnF                ColumnG
Customer1      ProcessActivitity1    Date
Customer2      ...                          ...
...

MyDateList(x) = Application.WorksheetFunction. _
       SumProduct(--([B:B] = Customer), --([F:F] = MyProcessList(x)),
--([G:G]))

Error message: "Incompatible types" (Hope the spelling is right :))

Thank you in advance! I am sure its a simple misstake I'v made, beginner and
all...

Take care!

//Jonas
Dave Peterson - 18 Apr 2008 12:45 GMT
=sumproduct() expects arrays as its arguments.

If you want to use application.worksheetfunction.sumproduct, then you'll have to
make those arguments arrays--maybe looping through each cell and returning 1's
or 0's.

And this destroys the idea around using =sumproduct()--why not just loop and
accumulate?

But you could let excel help you by using .Evaluate().  This is like using a
helper cell in the worksheet and letting excel calculate the result.

But when you do this, you have to follow excel's rules.  Unless you're using
xl2007, you can't use the complete column.

Here's a small example with the ranges hardcoded:

Option Explicit
Sub testme()
   Dim customer As String
   Dim myFormula As String
   customer = "asdf"
   
   myFormula = "sumproduct(--(b1:b9=""" & customer & """),g1:g9)"
   MsgBox ActiveSheet.Evaluate(myFormula)
   
End Sub

This is the formula that's being evaluated:
=sumproduct(--(b1:b9="asdf"),g1:g9)

If I used application.evaluate(), then the ranges on the activesheet would be
used.

I could have specified the addresses (including sheet names) or I could use:

workhsheets("sheet9999").evaluate(myformula)
if both ranges were on sheet9999

> Hi all, I am new to macros and VBA. Have a question concerning SumProduct.
>
[quoted text clipped - 19 lines]
>
> //Jonas

Signature

Dave Peterson

olssonj - 18 Apr 2008 14:35 GMT
Thanks Dave!!!

I feel that it must exist an easier way to match values in two (or more)
columns and returning the value of the same row in a third column. I am not
in the need of ackumulating values, only returning the actual date in each
case when two (or more) column values match indata statements.

I refuse to make long for- and/or if- statements ;) I want to learn to be
efficient :)

Appreciate your further assistance!!! Thanks for your patience!!!

//Jonas

> =sumproduct() expects arrays as its arguments.
>
[quoted text clipped - 58 lines]
> >
> > //Jonas
Dave Peterson - 18 Apr 2008 15:29 GMT
Maybe you can use Edit|find (in code, though) to search through the first
column.  Then check the second (and third and ...) columns for a match.  

If you match what you want, then retrieve the value from the other cell.

Or maybe you could apply Data|Filter|autofilter and extract the value from the
visible cell(s).

> Thanks Dave!!!
>
[quoted text clipped - 76 lines]
> >
> > Dave Peterson

Signature

Dave Peterson

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.