SumProduct in VBA in Excel 2010

Hi All
I want to use sumproduct to get a weighted average using named ranges because the data size changes from time to time.

I have this snippet:

 Dim a, b As Range
    a = Range("T" & Top & ":T" & Bottom)
    b = Range("S" & Top & ":S" & Bottom)

    Range("H5") = Application.SumProduct (a, b) / b

Top and Bottom are variables,  it crashes on the sumproduct line.
   

I would really appreciate someone with superior knowledge helping me out, thank you
nigelboyleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FarWestCommented:
what is the Top, Bottom , are they variables in your code?
0
Saurabh Singh TeotiaCommented:
Your syntax is incorrect the correct syntax will be...

Also assuming you have top and bottom defined and determined before coming to this part...

Option-1..

    Dim a As Range, b As Range
 Set a = Range("T" & Top & ":T" & Bottom)
    Set b = Range("S" & Top & ":S" & Bottom)
    
    Range("H5").Value = Application.WorksheetFunction.SumProduct(a, b) / Application.WorksheetFunction.Sum(b)

Open in new window


Option-2..

Dim a As Range, b As Range
   Set a = Range("T" & Top & ":T" & Bottom)
    Set b = Range("S" & Top & ":S" & Bottom)
    
    Range("H5").Value = Application.Evaluate("=Sumproduct(" & a.Address & "," & b.Address & ")/sum(" & b.Address & ")")

Open in new window


Both will give the same results also when you are diving by B you are not doing sum and if you want to calculate a weighted average you should do a sum of the same...

Saurabh...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NorieVBA ExpertCommented:
You can't divide by an array, which is what b is.

What would the formula look like on a worksheet?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

FarWestCommented:
you can use this
Public Sub b()
 Dim a As String, b As String
Dim top As String, bottom As String
top = "1"
bottom = "5"
 a = "T" & top & ":T" & bottom
 b = "S" & top & ":S" & bottom
    Range("H5").FormulaLocal = "=SumProduct(" & a & ";" & b & ") / Sum( " & b & ") "
End Sub

Open in new window

0
nigelboyleAuthor Commented:
First one worked perfectly, thanks
0
FarWestCommented:
but my solution makes it formula, so it will auto calculated when values changed

regards
0
nigelboyleAuthor Commented:
@ FarWest thanks for this, We auto create the worksheet from a database report tool and only run the macro once to organize the data. If the data changes the report is re-run
0
FarWestCommented:
aha, I see, then you are right the accepted answer is the best, and it will not have any performance impact when working on sheet
good luck
0
nigelboyleAuthor Commented:
@ FarWest - many thanks
0
NorieVBA ExpertCommented:
Nigel

Just curious, which of Saurabh's solutions did you use?
0
nigelboyleAuthor Commented:
@ Norie - the first - works well
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.