+ Reply to Thread
Results 1 to 15 of 15

How to multiply two ranges and use the result as an argument fo a worksheetfunction in VBA

  1. #1
    Registered User
    Join Date
    03-20-2012
    Location
    New York City
    MS-Off Ver
    Excel 2007
    Posts
    10

    How to multiply two ranges and use the result as an argument fo a worksheetfunction in VBA

    I am trying to mutliply two ranges together and use the result as a worksheet function in VBA. I get a "type mismatch" error when running this code in VBA. Here is the code:
    Please Login or Register  to view this content.
    Last edited by bgexcel; 03-22-2012 at 11:21 AM. Reason: Added Code tags

  2. #2
    Valued Forum Contributor
    Join Date
    03-14-2012
    Location
    Arizona USA
    MS-Off Ver
    Excel 2000/2007
    Posts
    408

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    To debug this, I would need the wb. The code is no good without sheets(funds,bench and weights) and the data they contain.

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    Get a working formula in a regular cell first. Once you get it working in a cell, share with us that working formula and we can suggest how to make it a VBA formula.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    03-20-2012
    Location
    New York City
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    Thanks for the quick replies. Assuming my work doesn't block it, I'll upload the workbook tomorrow (at home now). FYI, I already have the formula working in the workbook, just need to get it working in VBA...

  5. #5
    Registered User
    Join Date
    03-20-2012
    Location
    New York City
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    Here is the working formula in Excel:

    Please Login or Register  to view this content.
    Unfortunately I keep getting an error when I try to add the wb as an attachment to this post (xlsm file size is less than 1000k, its a differnet error).

    I just need to get this function working in VBA. All of the cells referenced and passed into the function contain numeric values.

    FYI, this code works in VBA:

    Please Login or Register  to view this content.
    This code does not work:

    Please Login or Register  to view this content.
    Last edited by bgexcel; 03-21-2012 at 10:41 AM.

  6. #6
    Valued Forum Contributor
    Join Date
    10-15-2007
    Location
    Home
    MS-Off Ver
    Office 2010, W10
    Posts
    373

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    Hi

    You are populating the cell with with the result of the formula. You can use the value that the formula returns:

    Please Login or Register  to view this content.
    Remark: In your last post you use Linest() with the 4th parameter equal to True. In that case you'd need a range of cells 5 rows * several columns to store the value of the array that Linest() returns.

    Does this help?

  7. #7
    Registered User
    Join Date
    03-20-2012
    Location
    New York City
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    I know how linest works (it contains an array of results, but by default it will return the coefficient for a simple regression if you don't use the index function to identify which stat you want returned). The VBA code returns an error because I'm trying to mutlipy a weight vector by my dependent and independent variables ("funds" and "bench" respectively). I think I get an error in VBA because of the vector multiplication (by the way, I'm not looking for a dot product, rather component wise mutliplication).

    Also, according to Microsoft help, even if you set the last parameter of LINEST to FALSE it will still return a array of values (and you can use the INDEX function to specify which you want).

    stats Optional. A logical value specifying whether to return additional regression statistics.
    If stats is TRUE, LINEST returns the additional regression statistics; as a result, the returned array is {mn,mn-1,...,m1,b;sen,sen-1,...,se1,seb;r2,sey;F,df;ssreg,ssresid}.

    If stats is FALSE or omitted, LINEST returns only the m-coefficients and the constant b.

  8. #8
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    Please try this:

    Please Login or Register  to view this content.
    I hope that helps.
    .?*??)
    `?.???.?*??)?.?*?)
    (?.?? (?.?
    Pichingualas <---
    ??????????????????????????

    Wrap your code with CODE TAGS.
    Thank those who helped you, Don't forget to add to their REPUTATION!!! (click on the star below their post).
    Please mark your threads as [SOLVED] when they are (Thread Tools->Mark thread as Solved).

  9. #9
    Registered User
    Join Date
    03-20-2012
    Location
    New York City
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    Thanks but it returns an error: "Unable to get the LinEst property of the WorksheetFunction class".

    Also, I do NOT want matrix multiplication. I need component wise multiplication. For instance, If I have two vectors:

    vector1 = (1, 2, 3)
    vector2 = (4, 5, 6)

    Then I want to pass vector3 = vector1 * vector2 = (4, 10, 18) into the LINEST function.

  10. #10
    Valued Forum Contributor
    Join Date
    10-15-2007
    Location
    Home
    MS-Off Ver
    Office 2010, W10
    Posts
    373

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    Hi bgexcel

    You are right. The error you get in VBA because of the vector multiplication.

    I just thought that since what you want is to populate the cell with the value of the formula it would be much easier to let the formula calculate the value and then use it.

    Sorry, I did not understant if you think it was a good idea and your problem is solved or if you still prefer to do it using the vba worksheetfunction method.

  11. #11
    Registered User
    Join Date
    03-20-2012
    Location
    New York City
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    I'm open to solutions but I need to pass the ranges (vectors) into the LINEST function dynamicly because I'm running this regression model on a bunch of different time series. If I can just get this part of the VBA to work I should be on my way

  12. #12
    Valued Forum Contributor
    Join Date
    10-15-2007
    Location
    Home
    MS-Off Ver
    Office 2010, W10
    Posts
    373

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    Well, if you had vba arrays you'd have to build their multipication using a loop. In this case, however, since you have ranges, you can evaluate their product.

    For a case like the one you posted you could use something like:

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    03-20-2012
    Location
    New York City
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    lecxe - that worked! I just didn't know the syntax for the multiplication in VBA.

    Thank you!

  14. #14
    Valued Forum Contributor
    Join Date
    10-15-2007
    Location
    Home
    MS-Off Ver
    Office 2010, W10
    Posts
    373

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    I'm glad it helped.

    Notice that that is not really a multiplication in vba, the [] are the shorthand for the Evaluate() method and what you are doing, in fact, is getting the result of a formula.

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to multiply two ranges and use the result as an argument fo a worksheetfunction in

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ 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