Consulting

Results 1 to 5 of 5

Thread: Sumproduct in VBA

  1. #1
    VBAX Regular
    Joined
    Apr 2009
    Posts
    16
    Location

    Sumproduct in VBA

    Hi there,

    Got two problems in one little segment of code!

    I want to enter the IF statement "If" output is "M1" or "M2" what syntax should I be using.

    Also I want to be able to use SUMPRODUCT in VBA but it does not seem to work the program just stops when I try to call it? Again what syntax should I use?

    If output = ("M1" Or "M") Then
    Dim Prob() As Double
    ReDim Prob(N)
    For i = 1 To N
        Prob(i) = Application.Max(p2 ^ i * (1 - p2) ^ (N - i))
    Next i
    M1 = Application.SumProduct(Prob, Val)
    M2 = Application.SumProduct(Prob, Val ^ 2)
    End If
    Thanks

    Baz

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    If output = "M1" Or output = "M" Then

    You cannot use SumProduct directly in VBA, you need to EVALUATE it.
    Have a look here for more info.
    http://www.xldynamic.com/source/xld.SUMPRODUCT.html
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    With OR and AND functions, you need to test both indivifually like so

    If output = "M1" Or output = "M" Then
    SUMPRODUCT is an array formula, so you cannot use Applictaion, you have to evaluate it

    M1 = Application.Evaluate("SumProduct(" & Prob & "," &  Val & ")")
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Oopos, didn't see Malcolm's response.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Apr 2009
    Posts
    16
    Location
    thanks!

    To all of you.

    Baz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •