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

Lookup Values Under Categories in Excel and Google Sheets

We can use a combination of XLOOKUP and VLOOKUP in both Excel and Google...

Extract Items Under a Category from the Same Column in Excel

In Excel, you can use a combination of the XLOOKUP and DROP functions to...

How to Create a Searchable Table in Excel Using the FILTER Function

Finding specific records, or rows containing the required information, is straightforward in Excel using...

Time Sequences in Excel by Minute, Hour, or Second Increments

Creating time sequences, whether by hour, minute, or second increments in Excel, can be...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

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.