Option Explicit Function MatrixProduct(A As Variant, B As Variant) As Variant 'Assumes that A,B are 1-based variant arrays Dim m As Long, n As Long, p As Long, i As Long, j As Long, k As Long Dim C As Variant If TypeName(A) = "Range" Then A = A.Value If TypeName(B) = "Range" Then B = B.Value m = UBound(A, 1) p = UBound(A, 2) If UBound(B, 1) <> p Then MatrixProduct = "Not Defined!" Exit Function End If n = UBound(B, 2) ReDim C(1 To m, 1 To n) For i = 1 To m For j = 1 To n For k = 1 To p C(i, j) = WorksheetFunction.Max((C(i, j)), (WorksheetFunction.Min((A(i, k)), (B(k, j))))) Next k Next j Next i MatrixProduct = C End Function Sub test() Dim start As Double Dim cases As Long Dim A As Variant, B As Variant, C As Variant A = Range("A1:TX544") B = Range("TZ1:AOW544") C = MatrixProduct(A, B) Range("A546:TX1089").Value = C End Sub