HSTACK Function in Google Sheets (How To)

The HSTACK function in Google Sheets is used to append ranges horizontally. While we can use curly braces for this purpose, they cannot fully replace the HSTACK function.

Do you know why?

Curly braces require that all the ranges being appended have the same number of rows.

However, with the HSTACK function, you can use arrays of any size. It does not require the number of rows or columns in the appended ranges to match.

I find the VSTACK function (vertical stacking) more practical than its sibling’s HSTACK (horizontal stacking).

Syntax of the HSTACK Function

Syntax: HSTACK(range1, [range2, …])

  • range1: The first array or range to append.
  • range2, …: Additional arrays or ranges (optional) to append to the first range.

Example

I have three tables and want to append them horizontally into a single array.

The table ranges are A3:D4, A6:C8, and A9:D10.

HSTACK function appending ranges horizontally in Google Sheets

The HSTACK formula in cell A13 returns the single appended array in A13:K15.

=HSTACK(A3:D4, A6:C8, A9:D10)

Why are there some #N/A values in the result?

As you can see, the number of rows and columns in each table is different.

But the HSTACK function has no issue appending them horizontally.

The output of the HSTACK formula will have rows equal to the maximum number of rows from the appended ranges

In this process, it adds #N/A values in some cells to fill the gaps where rows are missing.

To remove these #N/A values, wrap the formula with an IFNA function.

=IFNA(HSTACK(A3:D4, A6:C8, A9:D10))

HSTACK with SUMIF Function in Google Sheets (Real-Life Example)

Now that we’ve learned how the HSTACK function works in Google Sheets, let’s see a real-life example of using it with the SUMIF function to generate a summary table.

Assume we have a product list in column A and their quantities in column B.

How do we create a summary from this data?

HSTACK formula used in a real-life example in Google Sheets

Of course, the following QUERY formula will do that:

=QUERY(A:B, "SELECT A, SUM(B) WHERE A IS NOT NULL GROUP BY A", 1)

Alternatively, we can use UNIQUE and SUMIF functions combined with HSTACK, as shown below:

=HSTACK(
   TOCOL(UNIQUE(A2:A), 3), 
   INDEX(SUMIF(A2:A, TOCOL(UNIQUE(A2:A), 3), B2:B))
)

Let’s break this formula down:

  • range1: TOCOL(UNIQUE(A2:A), 3)
    This returns the unique items from column A without blanks.
  • range2: INDEX(SUMIF(A2:A, TOCOL(UNIQUE(A2:A), 3), B2:B))
    This returns the sum of quantities for each unique product.

The HSTACK function appends these two ranges horizontally, giving us a summary of products and their total quantities.

I hope the above examples help you understand how to use the HSTACK function in Google Sheets effectively.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Calculate Trip Days by Month (Start, End, and Full Days) in Google Sheets

If you're managing business travel in Google Sheets, you may need to calculate how...

Mode of Comma-Separated Numbers in Excel (Dynamic Array)

There is no dedicated function in Excel to directly find the mode of comma-separated...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

How to Count Merged Cells in Google Sheets (and Get Their Size)

Sometimes, you may have blocks of merged cells in a column or row. But...

More like this

Calculate Trip Days by Month (Start, End, and Full Days) in Google Sheets

If you're managing business travel in Google Sheets, you may need to calculate how...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

How to Count Merged Cells in Google Sheets (and Get Their Size)

Sometimes, you may have blocks of merged cells in a column or row. But...

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.