HomeGoogle DocsSpreadsheetProper Use of MMULT in Infinite Rows in Google Sheets

Proper Use of MMULT in Infinite Rows in Google Sheets

Published on

First of all, using ISBLANK or LEN with MMULT is not a practical solution to correctly refer to infinite range reference in MMULT function in Google Sheets. In this post, let me share with you a very useful MMULT related tip/formula that will help you to use MMULT in infinite rows in Google Sheets.

Let me clarify why ISBLANK or LEN is not a suitable solution to use MMULT in infinite rows.

We are accustomed to using ISBLANK as well as LEN to limit the expanding of array formula outputs to the rows containing values in any specific column. This will work in MMULT too but there is one limitation.

Before coming to that see the below MMULT formula that sums across rows in a closed/limited range, i.e. A2:C6.

=mmult(A2:C6,{1;1;1})

MMULT Syntax: MMULT(matrix1, matrix2)

Note: When you want to sum across rows using MMULT, use the matrix 2 in MMULT as above. The matrix2 contains only the numbers 1 and the count of the same must be equal to the number of columns in matrix1.

I hope the above formula is simple enough for you to understand. But an expert won’t use the MMULT formula as above. Why?

Just delete any value in the range A2:C6. The MMULT would return an error! To sort out that error, use the MMULT as below.

N Function in MMULT to Sort Out Blank Cell Issue

=ArrayFormula(mmult(n(A2:C6),{1;1;1}))

The N function converts blank cells to 0 while retaining the numbers as it is. The ArrayFormula use is justified as function N is not an array formula.

Now let’s concentrate on our real problem, i.e. how to use MMULT in infinite rows (some people say open range) in Google Sheets.

I want to replace the matrix1 in the above formula which is A2:C6 with an infinite range like A2:C. How to do that?

I have already mentioned above that the ISBLANK or LEN approach won’t work here. First, understand the said two approaches then I will explain how to properly use MMULT in infinite rows or you can say infinite range in Google Sheets.

Methods to Use MMULT in Infinite Rows/Open Range in Google Sheets

LEN with MMULT in Open Range

Normally you may think about using the LEN formula as below with MMULT.

=ArrayFormula(If(len(A2:A),mmult(n(A2:C),{1;1;1}),))

This MMULT formula will definitely work in infinite rows in Google Sheets. But it has a limitation as below.

That means if there is any blank cell between the data range in the first column (the LEN applied column), MMULT will ignore that row in the calculation. The same is the case with ISBLANK with MMULT.

ISBLANK with MMULT in Open Range

Use ISBLANK as below in MMULT to handle infinite Rows in MMULT in Google Sheets, but with the above said limitation.

=ArrayFormula(If(isblank(A2:A)=FALSE,mmult(n(A2:C),{1;1;1}),))

Time to think about a proper formula to use MMULT in infinite rows.

The Proper Way to Use MMULT in Infinite Rows

Formula (this is not my final formula):

=ArrayFormula(mmult(N(array_constrain(A2:C,MATCH(2,1/(A2:A<>""),1),3)),{1;1;1}))

Explanation:

Before going to explain the above formula let me share with you how to populate matrix2 dynamically.

You can replace {1;1;1} with sequence(columns(A2:C2),1)^0. This Sequence generated matrix2 will be useful if your matrix1 has a very large number of columns. I am incorporating this matrix2 formula to the above formula.

=ArrayFormula(mmult(N(array_constrain(A2:C,MATCH(2,1/(A2:A<>""),1),3)),sequence(columns(A2:C2),1)^0))
Correct Use of MMULT in Infinite Rows in Google Sheets

The only part now left to explain in the formula is the matrix1 which is N(array_constrain(A2:C,MATCH(2,1/(A2:A<>""),1),3))

If you test this formula separately, please include ArrayFormula as below.

=ArrayFormula(N(array_constrain(A2:C,MATCH(2,1/(A2:A<>""),1),3)))

In this, the Match formula returns the number of rows in the range, which is 5.

=ArrayFormula(MATCH(2,1/(A2:A<>"")))

Related Article (but in Excel, equally works well in Sheets): Address of the Last Non-Empty Cell Ignoring Blanks in a Column in Excel.

Regarding the Array_Constrain, it limits the number of rows in A2:C to 5 (match formula output) rows and 3 columns and that is the MMULT matrix1.

Syntax of ARRAY_CONSTRAIN (to make you understand the formula explanation):

ARRAY_CONSTRAIN(input_range, num_rows, num_cols)

input_range – A2:C
num_rows – MATCH(2,1/(A2:A<>""))
num_cols – 3

To bring more dynamism, you can replace 3 (num_cols) with columns(A2:C2) and the final formula to use MMULT in infinite Rows in Google Sheets is;

=ArrayFormula(mmult(N(array_constrain(A2:C,MATCH(2,1/(A2:A<>""),1),columns(A2:C))),{1;1;1}))

In my example, matrix1 is the open range. In your case, if matrix2 is the open range, you can follow the above matrix1 steps with matrix2 and vice versa. That’s all. Enjoy!

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

More like this

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to the...

3 COMMENTS

  1. I have noted that if you put =ArrayFormula(row(A1:A)) formula in A2 the rows keep expanding till it reaches 50500!!!

    I feel this is a bug.

    Is there any way to stop this?

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.