When multiplying matrices, the number of columns of the first matrix must be the same as the number of rows in the second matrix. Diagonal() is returning a 6x6 matrix. MMULT() is then trying to multiply that 6x6 matrix by a 5x3 matrix which is not possible.
If I go into Diagonal() and replace the intN statement so that it is 5 (intN=5), the MMULT() formula in G7:I9 runs without error. If we can get Diagonal() to correctly determine the size of the output array, everything should work fine.
I'm not certain of the strategy here. The current strategy is to parse the Range.Address property and determine the size of the input vectors using the spreadsheet row number. It seems that this would only work when the input range includes spreadsheet row 1. In this case, your input ranges are starting in row 2. I would normally prefer to count the number of rows rather than read the address string for this. Something like intN=rngA.rows.count. Then I can pass any vector from anywhere in the spreadsheet to the function.
Bonus observation: I cannot think of any good reason to use ActiveSheet to bring any information into a UDF. Sure it works okay as long as sheet1 is the active sheet, but the formula will error or return unexpected results as soon as you navigate to a different tab in the workbook or a different workbook. ActiveSheet means whatever sheet is currently active at runtime, which may or may not be the same sheet where you have entered the UDF. You have passed the necessary information to the function in rngA, rngB, rngC. I would refer to those object variables rather than Active sheet.
For I = 1 To intN
For J = 1 To intN
If I = J Then
'strD = strA & Format(I + 1)
'strE = strB & Format(I + 1)
'strF = strC & Format(I + 1)
'TempArray(I, J) = ActiveSheet.Range(strD).Value * ActiveSheet.Range(strE).Value * ActiveSheet.Range(strF).Value
TempArray(I,J)=rngA.cells(I,1)*rngB.Cells(I,1)*rngC.Cells(I,1)
Else
TempArray(I, J) = 0#
End If
Next J
Next I
This obviates the need for the strA etc. variables and allows VBA to correctly calculate the UDF even when this sheet is not the active sheet.
See how that fits into your broader project.
Bookmarks