+ Reply to Thread
Results 1 to 6 of 6

Code to Calculate the product of two matrices won't calculate

  1. #1
    Registered User
    Join Date
    09-06-2015
    Location
    Chicago, IL
    MS-Off Ver
    2011 for Mac
    Posts
    2

    Code to Calculate the product of two matrices won't calculate

    Hello,

    Yes, I'm a student with an assignment.
    But, I fell like I'm most of the way to my solution, although my code isn't working.
    I have written the code below to calculate the product of two matrices but I keep getting #NAME? instead of a matrix.

    I'm running in Excel - Microsoft Visual Basic 14.5.5 for Mac

    Any help would be much appreciated!
    Thanks

    Please Login or Register  to view this content.
    Attached Images Attached Images
    Last edited by njrobby; 10-05-2015 at 12:02 AM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,885

    Re: Code to Calculate the product of two matrices won't calculate

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (Because you are new to the forum, I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Code to Calculate the product of two matrices won't calculate

    You don't get any compile errors? I immediately got two compile errors. One for "duplicate declaration" for the Dim A... statement (A is passed to the function in the argument list, too). Another compile error was for the Exit For not within For...Next loop for the third Exit For statement, which is outside of the loop. If you are getting any error statements, be sure to mention them as that is an important part of debugging. I am guessing that both of these statements are superfluous and should be deleted.

    The other thing I notice is that you are not defining the size of the C() array anywhere. This will cause a runtime error, in the VBA does not know what size this array is. Either include the limits in your Dim C()... statement, or include a ReDim statement that will set the size of this array before it is used.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Registered User
    Join Date
    09-06-2015
    Location
    Chicago, IL
    MS-Off Ver
    2011 for Mac
    Posts
    2

    Re: Code to Calculate the product of two matrices won't calculate

    I did notice the Dim A and Exit for and ' those out already.
    I'm new to this whole thing and didn't realize I could do that until after I posted
    As for the C() array, I thought that the function would just calculate the first cell and then I shift + enter for the rest?
    I've tried it by entering just the first cell and by creating the 3x3 array. But, I got the error both ways.
    How would I set the ReDim?

    Thanks

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Code to Calculate the product of two matrices won't calculate

    Help file for the redim statement: https://msdn.microsoft.com/en-us/library/w8k3cys2.aspx

    The function will not just calculate and figure out how big to make the C() array. You have to tell it. If you want it to figure out how large based on the size of the output array, then you can use caller.parent (I think that's it) to determine the size of the output array. If you want to figure out how large the array should be from the input arrays, then use what you know of matrix multiplication to determine the size of the array. According to this http://www.mathsisfun.com/algebra/ma...ltiplying.html the output array will have the same number of columns as the first matrix and the same number of rows as the second matrix, so you could put those values into a redim statement. Or, you can dimension the C array to be larger than any array you ever expect to need. However you choose to do it, VBA is not going to magically know how large to make C(). The programmer needs to somehow instruct VBA how large it will be or how to figure out how large it will be.

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: Code to Calculate the product of two matrices won't calculate

    Functions must be in a general module not a sheet module
    C(i, j) has no dimensions, C must be redim'd to have the same number of rows as matrix A and the same number of columns as matrix b:

    ReDim C(1 to a.Rows.count, 1 to b.Columns.Count)

    You have an Exit For after the Next k statement but since it is inside the j loop it causes an exit from the J loop after the first iteration, i.e., k loops 1 to 3 but you exit j before the next j statement The statement after the next j is another exit, this time within the i loop so that it causes exit from the i loop. finally, after the next i you have another exit for, but this one is outside of all the loops and refers to nothing.

    You never use "exit for" to exit a loop unless you are testing a condition within the loop and you want to exit if the condition is satisfied:

    For x = 1 to 10
    If x = 7 then Exit for
    Next x

    For x = 1 to 10
    do something
    next x
    Exit For

    This is always wrong and the code will crash

    User defined functions are meant to return a calculated value to a worksheet cell or to a variable in some other VB module. In order to return the value, you must set the function to the value within your function's code, usually as the last line:

    Function TimesTwo(a as range) as Double
    TimesTwo=a*2
    End Function
    or

    Function MultMatrix(...etc...) as Variant
    {calculations}
    Multmatrix = C
    End Function
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro add-in to calculate product of cells
    By Bala Daniel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-03-2014, 04:34 PM
  2. Calculate Weighted Average using Sum product and Conditions
    By sathishpalaniswamy in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 02-25-2014, 05:06 AM
  3. [SOLVED] VBA to calculate Product weights
    By elgato74 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-24-2014, 06:31 PM
  4. How to calculate standard deviation of a product?
    By Serina90 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-01-2013, 10:26 AM
  5. Calculate cross product of 2 vectors
    By tjsdndnjs in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-29-2012, 03:09 AM
  6. [SOLVED] calculate sum of the product from one of the column
    By top1 in forum Excel General
    Replies: 12
    Last Post: 09-07-2012, 12:14 AM
  7. [SOLVED] IF Formula to calculate if product has been sent on time
    By david1987 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-06-2012, 05:22 AM
  8. [SOLVED] How do I calculate cost of raw materials to final product?
    By just desserts in forum Excel General
    Replies: 2
    Last Post: 01-28-2006, 03:20 PM

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