+ Reply to Thread
Results 1 to 5 of 5

Run a SUMPRODUCT in VBA

  1. #1
    Registered User
    Join Date
    10-12-2007
    Posts
    36

    Run a SUMPRODUCT in VBA

    H guys,
    Can I get some help with the following:

    As part of a Sub id like to run the following function and save it to a variable.

    Please Login or Register  to view this content.
    How can I do it?

    I've tried this but I get a Type Mismatch error:

    Please Login or Register  to view this content.
    And also this:

    Please Login or Register  to view this content.
    Again same Type Mismatch error!

    Can I get some help please!

    Thanks
    Chris

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Run a SUMPRODUCT in VBA

    In your first attempt you have a superfluous closing parentheses which is the cause of the error.

    Generally speaking replicating worksheet SUMPRODUCTs from VBA are conducted via Evaluate.
    (whether you opt to use [ ] rather than Evaluate( ) is something of a personal choice though the two can generate different results)

  3. #3
    Registered User
    Join Date
    10-12-2007
    Posts
    36

    Re: Run a SUMPRODUCT in VBA

    Thanks DonkeyOte,

    How do I go about doing it via Evaluate then please?

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Run a SUMPRODUCT in VBA

    Your first example is using Evaluate just in "shorthand" form [ ]

    The equivalent longer version would be:

    Please Login or Register  to view this content.
    note the double up of quotes... for this reason some prefer the [ ] method.

    Evaluate will evaluate any unqualified ranges against the active sheet at run time unless otherwise stipulated.
    It follows that if you want your SUMPRODUCT to refer to ranges on a sheet other than the active sheet at run time you should qualify the appropriate sheet either by

    a) stipulating the relevant sheet in the SUMPRODUCT itself

    Please Login or Register  to view this content.
    or

    b) qualifying the Evaluate call

    Please Login or Register  to view this content.
    As mentioned the [ ] and Evaluate( ) methods can generate different outputs (though not necessarily relevant here), example:

    Please Login or Register  to view this content.
    v1 generates a string whereas v2 generates a 2 dimensional variant array

  5. #5
    Registered User
    Join Date
    10-12-2007
    Posts
    36

    Re: Run a SUMPRODUCT in VBA

    thank you mate - makes absolute sense. its no wonder you are a guru!
    ive just tried both [] and evaluate and it gives me the same results so ill stick to [] cause its a little bit cleaner. in any case though your explanation was spot on.
    cheers
    chris

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1