Consulting

Results 1 to 8 of 8

Thread: SUMPRODUCT in VBA

  1. #1

    SUMPRODUCT in VBA

    Hello everyone
    I am stuck of the use of the SUMPRODUCT in VBA
    I have this formula
    =SUMPRODUCT((SH!$D$12:$D$22="Male")*(SH!$E$12:$E$22=1))
    And I used variables for ranges :
    rGender = SH!$D$12:$D$22
    rGrade =SH!$E$12:$E$22

    When I tried this line it failed
    MsgBox Application.WorksheetFunction.SumProduct(rGender, strMale, rGrade, grade)
    Any help please ....

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    You need to use .Evaluate

    Try this


    Option Explicit
    
    'ref: http://www.xldynamic.com/source/xld.SUMPRODUCT.html
    
    Sub demo()
        Dim rGender As Range, rGrade As Range
        Dim strMale As String, strFormula As String
        Dim iGrade As Long
        
        strMale = "Male"
        iGrade = 1
        Set rGender = Worksheets("SH").Range("$D$12:$D$22")
        Set rGrade = Worksheets("SH").Range("$E$12:$E$22")
    
        '=SUMPRODUCT(($D$12:$D$22="Male")*($E$12:$E$22=1))
    
        'note the extra quotes around strMale
        strFormula = "=SUMPRODUCT((" & rGender.Parent.Name & "!" & rGender.Address & "=""" & strMale & """)*(" & rGrade.Parent.Name & "!" & rGrade.Address & "=" & iGrade & "))"
    
        MsgBox strFormula
        MsgBox Application.Evaluate(strFormula)
    
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 12-30-2016 at 04:02 PM. Reason: Add attachment
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    MsgBox WorksheetFunction.CountIfs(Worksheets("SH").Range("$D$12:$D$22"), "Male", Worksheets("SH").Range("E12:E22"),1 )

  4. #4
    Thank you very much for these perfect solutions
    I wonder why can't sumproduct used directly in VBA although it is built in worksheetfunction .. that's weird

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    You can call it via VBA, but I don't think you can use it the way you wanted to


    Option Explicit
    Sub Macro1()
        Range("A1:A10").Value = 2
        Range("B1:B10").Value = 3
        Range("C1:C10").Value = 4
        Range("D1:D10").Value = 7
        Range("E1:E10").Value = 11
        
        MsgBox Application.WorksheetFunction.SumProduct(Range("A1:A10"), Range("B1:B10"), Range("C1:C10"), Range("D1:D10"), Range("E1:E10"))
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Thanks a lot Mr. Paul for illustration ..

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by YasserKhalil View Post
    Thank you very much for these perfect solutions
    I wonder why can't sumproduct used directly in VBA although it is built in worksheetfunction .. that's weird
    When you use SUMPRODUCT for conditional counting/summing, you are effectively using an array function even though you don't CSE it, and you cannot just execute an array function in VBA, you need to evaluate it, so you need to do the same with SUMPRODUCT.
    ____________________________________________
    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

  8. #8
    Thanks a lot xld for your reply and for this useful information
    Best Regards

Posting Permissions

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