HomeGoogle DocsSpreadsheetHSTACK Function in Google Sheets (How To)

HSTACK Function in Google Sheets (How To)

Published on

The HSTACK function in Google Sheets is for appending ranges horizontally. Even though we can use Curly Braces for this purpose, it can’t substitute the HSTACK function.

Do you know the reason?

The Curly Braces requires all the ranges to append must contain an equal number of rows.

But in the HSTACK function, we can use arrays of any size. It does not necessarily match the columns and rows in the appending ranges.

I find VSTACK (vertical stacking) more practical when compared to HSTACK (horizontal stacking), its sibling.

Syntax of the HSTACK Function

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

range1: The first array or range to append.

range2, … : Additional arrays or ranges (optional) to add to the previous range.

Example

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

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

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

=hstack(A3:D4,A6:C8,A10:D11)
HSTACK Function - Basic Example

What’s the reason for a few #N/A values in the result?

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

But the HSTACK function has no issue in appending them horizontally.

The HSTACK formula output will have the number of rows matching the max number of rows in the appended ranges.

In the process, it adds #N/A values in some cells.

To remove them, wrap the formula with an IFERROR.

=iferror(hstack(A3:D4,A6:C8,A10:D11))

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

We have learned how the HSTACK function works in Google Sheets.

In the following real-life use example, I will use it with SUMIF to generate a summary table.

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

How do we make a summary out of it?

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 as follows.

=hstack(tocol(unique(A2:A),3),index(sumif(A2:A,tocol(unique(A2:A),3),B2:B)))
HSTACK Formula in Real-Life

We can split and learn this last formula.

range1: tocol(unique(A2:A),3)

range2: index(sumif(A2:A,tocol(unique(A2:A),3),B2:B))

The range1 in the above HSTACK formula returns the unique items without blanks.

The range2 returns the summary of the unique items.

I hope the above examples help you learn the use of the HSTACK function in Google Sheets.

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.

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...

XLOOKUP in Excel: Working with Visible Rows in a Table

In Excel, the XLOOKUP function isn't inherently designed to exclusively work with visible rows;...

More like this

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...

Analyzing Column A Between Non-Adjacent Values in Column B: Google Sheets

This tutorial addresses a common scenario encountered in Google Sheets: how to sum, average,...

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.