title: Function mimicing Excel MMULT
body: <p>I wanted to create function that mimics standard Excel MMULT function for multiplying matrices. My code is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>Function MatrixMultiplication(Matrix1 As Range, Matrix2 As Range)
  
  Dim m1() As Long, m2() As Long, m3() As Long
  m1 = Matrix1
  m2 = Matrix2
  
  If Matrix1 Is Nothing Or Matrix2 Is Nothing Then GoTo Err1
  If UBound(m1, 2) &lt;&gt; UBound(m2, 1) Then GoTo Err2
  
  ReDim m3(UBound(m1, 1), UBound(m2, 2))
  
  For i = LBound(m1) To UBound(m1, 1)
      For j = LBound(m2) To UBound(m1, 2)
          For k = 1 To UBound(m1, 2)
          m3(i, j) = m3(i,j)+ m1(i, k) * m2(k, i)
          Next k
      Next j
  Next i
  
  Dim Matrix3 As Range
  
  Set Matrix3 = Range(ActiveCell.Address, ActiveCell.Offset(UBound(m1, 1) - 1, UBound(m2, 2) - 1))
  
  Matrix3 = m3
  
  Set MatrixMultiplication = Matrix3
  
  Err1:
      Selection.Cells(0, 0).Value = CVErr(xlErrNull)
  Err2:
      Selection.Cells(0, 0).Value = CVErr(xlErrNA)
  
  End Function</code></pre>
  </div>
  </div>
  </p>
  
  <p>Somehow it does not work. It should work as CSE function. Thanks for any help.</p>