VSTACK Function In Google Sheets (How To)

The VSTACK function appends two or more ranges vertically and returns a larger array. It has become one of the most commonly used functions in Google Sheets. You can see its application in both simple formulas and more complex LAMBDA functions, such as REDUCE.

When using the VSTACK function in Google Sheets, you may encounter #N/A errors, blank cells, or blank rows in the output.

In this Google Sheets tutorial, you’ll learn how to use the VSTACK function and how to handle errors or remove blank rows from the output.

Why Use VSTACK Function Instead of Curly Braces?

Another option for stacking data vertically is using curly braces {}. While effective, curly braces come with some limitations. Instead of focusing on these limitations, we’ll highlight the advantages of using VSTACK over curly braces:

  • Readability: The VSTACK function makes formulas easier to read, especially when working with large datasets.
  • No need for semicolons: With VSTACK, you can use the standard argument separator (commas or semicolons, depending on your locale), unlike curly braces where semicolons are required to separate arrays.
  • Handling different-sized arrays: If the arrays have different widths, curly braces return a #VALUE! error, whereas VSTACK can handle arrays with varying numbers of columns (though it may return #N/A errors for missing values).

VSTACK Function: Syntax and Arguments

Syntax:

VSTACK(range1, [range2, …])

Arguments:

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

Basic Formula Examples

Here’s a simple example of stacking two equal-sized ranges:

=VSTACK(Sheet1!A2:C10, Sheet2!A2:C10)

In the following example, the first range contains two columns, and the second range contains three columns:

=VSTACK(B3:C5, E3:G5)
Basic examples of the VSTACK function in Google Sheets

In this case, VSTACK automatically adjusts to return the maximum number of columns between the two ranges. However, it fills the extra columns with #N/A errors when the number of columns doesn’t match. How can we remove these errors?

Removing #N/A Errors in VSTACK

Since VSTACK doesn’t have a pad_with argument (unlike the WRAPCOLS or WRAPROWS functions), we need to use the IFNA function to replace #N/A with a value like 0, a blank, or any other desired value.

Here’s how to replace #N/A with 0:

=IFNA(VSTACK(B3:C5, E3:G5), 0)

Handling Blank Rows in VSTACK

A common issue arises when stacking multiple QUERY results with VSTACK—you might end up with blank rows or rows full of #N/A values between the results. This happens when one or more QUERY formulas return no matching results.

Example (Formula in cell B9):

=VSTACK(
   QUERY(B2:C6, "SELECT * WHERE Col1 = 'a'"),
   QUERY(E2:F6, "SELECT * WHERE Col1 = 'b'"),
   QUERY(H2:I6, "SELECT * WHERE Col1 = 'c'")
)
Appending QUERY results with VSTACK and handling errors and blank rows in Google Sheets


If the second QUERY returns no results (i.e., no rows where Col1 = ‘b’), VSTACK leaves a row of #N/A values.

To replace #N/A with blanks, you can use IFNA:

=IFNA(
   VSTACK(
      QUERY(B2:C6, "SELECT * WHERE Col1 = 'a'"),
      QUERY(E2:F6, "SELECT * WHERE Col1 = 'b'"),
      QUERY(H2:I6, "SELECT * WHERE Col1 = 'c'")
   )
)

But this can still leave a blank row. How do we remove such blank rows?

Removing Blank Rows in VSTACK

We can use the LET function to define the VSTACK result and then filter out the blank rows.

=LET(
   result, 
   IFNA(
      VSTACK(
         QUERY(B2:C6, "SELECT * WHERE Col1 = 'a'"),
         QUERY(E2:F6, "SELECT * WHERE Col1 = 'b'"),
         QUERY(H2:I6, "SELECT * WHERE Col1 = 'c'")
      )
   ),
   FILTER(result, INDEX(result, 0, 1) <> "")
)

VSTACK Function with REDUCE Function

This section uses the LAMBDA function and is intended for advanced users.

The REDUCE function applies a LAMBDA function to an array, reducing it row by row while storing intermediate values in an accumulator. If you want to keep track of these intermediate values, you can use VSTACK to “stack” the accumulator value after each iteration.

Example:

Assume we have a list of items in A2:A. First, we extract unique items from this list:

=UNIQUE(A2:A)

Now, let’s use REDUCE to apply a LAMBDA function to these unique values and stack the results using VSTACK:

=REDUCE(TOCOL(,1), B2:B4, LAMBDA(acc, val, VSTACK(acc, FILTER(A2:A, A2:A = val),"*")))
Using VSTACK within the REDUCE function in Google Sheets

In this formula:

  • REDUCE iterates over each value in B2:B4.
  • For each value, it filters the range A2:A to match the current value (val).
  • VSTACK then stacks the accumulator (acc) with the filtered result, placing a "*" between each filtered result for clarity.

This method allows us to progressively accumulate and display the results, with each iteration adding new filtered results and a separator (“”) between them.

Conclusion

The VSTACK function is a powerful tool for appending data vertically in Google Sheets, offering better flexibility and readability than curly braces. By combining it with functions like IFNA, IFERROR, and LET, you can handle errors and blank rows more effectively.

Resources

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.

How to Extract Numbers from Text in Excel with Regex

You can use the REGEXEXTRACT or REGEXREPLACE functions to easily extract numbers from text...

Using OFFSET and MATCH Together in Google Sheets: Advanced Tips

One powerful and flexible way to look up values is by combining the OFFSET...

How to Use OFFSET and XMATCH Functions Together in Excel

We often use the OFFSET and XMATCH functions together to match a value in...

How to Calculate Maximum Drawdown in Excel and Google Sheets

You can use the following dynamic array formula to calculate maximum drawdown (MDD) in...

More like this

Using OFFSET and MATCH Together in Google Sheets: Advanced Tips

One powerful and flexible way to look up values is by combining the OFFSET...

Running Count with Structured References in Google Sheets

Running a count with structured references is achievable in Google Sheets tables using the...

Running Total with Structured Table References in Google Sheets

You can use two types of formulas to create a running total with structured...

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.