How to use the VSTACK function
The VSTACK function lets you combine cell ranges or arrays, it joins data to the first blank cell at the bottom of a cell range or array. The VSTACK function is available to Excel 365 users and is in the "Array manipulation" category.
What is VSTACK an abbreviation of?
VSTACK stands for vertical stacking meaning arrays are arranged on top of each other.
What is an array?
An array is an array of values meaning multiple values in its simplest form. Excel functions may accept an array as the input value or argument.
Some Excel functions return an array of values instead of a single value. Excel 365 handles these arrays automatically whereas previous versions need to be entered as an array formula. For example, the FREQUENCY function returns multiple values.
You can also create a formula that returns multiple values, in Excel 365 they are named dynamic array formulas and in earlier versions named array formulas. The most simple dynamic array formula I know is this: = A1:A2, it returns two values populated in cells A1 and A2.
Table of Contents
1. Syntax
The VSTACK function has one required arument and the remaining aruments are optional.
VSTACK(array1,[array2],...)
2. Arguments
array1 | Required. The first cell range or array. |
[array2] | Optional. The second cell range or array to merge. |
3. Example
The image above demonstrates how the VSTACK function merges the ranges B2:D4 (blue) and F2:H4 (red). It appends the second cell range (red) to the bottom of the first cell range (blue).
Formula in cell E4:
The VSTACK function is great for consolidating data from multiple worksheets, it is easy to use. Keep in mind to not reference the table headers or you will get table headers from each cell reference in the returning array. Check out formulas that use VSTACK function.
3.1 Explaining formula
Tip! You can follow the formula calculations step by step by using the "Evaluate" tool located on the Formula tab on the ribbon. This makes it easier to spot errors and understand the formula in greater detail. The following steps shows the calculations in great detail.
Step 1 - VSTACK function
VSTACK(array1,[array2],...)
Step 2 - Populate arguments
array1 - B2:D4
[array2] - F2:H:4
Step 3 - Evaluate function
VSTACK(B2:D4, F2:H:4)
returns
{89, 68, ... , 70}
4. VSTACK Function errors
The image above demonstrates what happens when you try to append two cell ranges containing a different number of columns. The first cell range (blue) has three columns and the second cell range (red) has 2 columns.
The result is an array containing #N/A errors in locations where no value exists.
Formula in cell E4:
The IFNA function lets you remove #N/A errors.
Formula in cell B8:
The array in cell B8 is now empty of #N/A! errors, see the image above.
4.1 Explaining formula
Step 1 - Stack cell ranges vertically
VSTACK(B2:D4, F2:G:4)
returns {89, 68, ... , #N/A}
Step 2 - Remove #N/A errors
IFNA(VSTACK(B2:D4, F2:G:4), "")
returns {89, 68, ... , 31, ""}
4.2 Other errors
The VSTACK function propagates errors, meaning the function will return an error if the source data contains an error.
4.3 Troubleshooting the error value
When you encounter an error value in a cell a warning symbol appears, displayed in the image above. Press with mouse on it to see a pop-up menu that lets you get more information about the error.
- The first line describes the error if you press with left mouse button on it.
- The second line opens a pane that explains the error in greater detail.
- The third line takes you to the "Evaluate Formula" tool, a dialog box appears allowing you to examine the formula in greater detail.
- This line lets you ignore the error value meaning the warning icon disappears, however, the error is still in the cell.
- The fifth line lets you edit the formula in the Formula bar.
- The sixth line opens the Excel settings so you can adjust the Error Checking Options.
Here are a few of the most common Excel errors you may encounter.
#NULL error - This error occurs most often if you by mistake use a space character in a formula where it shouldn't be. Excel interprets a space character as an intersection operator. If the ranges don't intersect an #NULL error is returned. The #NULL! error occurs when a formula attempts to calculate the intersection of two ranges that do not actually intersect. This can happen when the wrong range operator is used in the formula, or when the intersection operator (represented by a space character) is used between two ranges that do not overlap. To fix this error double check that the ranges referenced in the formula that use the intersection operator actually have cells in common.
#SPILL error - The #SPILL! error occurs only in version Excel 365 and is caused by a dynamic array being to large, meaning there are cells below and/or to the right that are not empty. This prevents the dynamic array formula expanding into new empty cells.
#DIV/0 error - This error happens if you try to divide a number by 0 (zero) or a value that equates to zero which is not possible mathematically.
#VALUE error - The #VALUE error occurs when a formula has a value that is of the wrong data type. Such as text where a number is expected or when dates are evaluated as text.
#REF error - The #REF error happens when a cell reference is invalid. This can happen if a cell is deleted that is referenced by a formula.
#NAME error - The #NAME error happens if you misspelled a function or a named range.
#NUM error - The #NUM error shows up when you try to use invalid numeric values in formulas, like square root of a negative number.
#N/A error - The #N/A error happens when a value is not available for a formula or found in a given cell range, for example in the VLOOKUP or MATCH functions.
#GETTING_DATA error - The #GETTING_DATA error shows while external sources are loading, this can indicate a delay in fetching the data or that the external source is unavailable right now.
4.4 The formula returns an unexpected value
To understand why a formula returns an unexpected value we need to examine the calculations steps in detail. Luckily, Excel has a tool that is really handy in these situations. Here is how to troubleshoot a formula:
- Select the cell containing the formula you want to examine in detail.
- Go to tab “Formulas” on the ribbon.
- Press with left mouse button on "Evaluate Formula" button. A dialog box appears.
The formula appears in a white field inside the dialog box. Underlined expressions are calculations being processed in the next step. The italicized expression is the most recent result. The buttons at the bottom of the dialog box allows you to evaluate the formula in smaller calculations which you control. - Press with left mouse button on the "Evaluate" button located at the bottom of the dialog box to process the underlined expression.
- Repeat pressing the "Evaluate" button until you have seen all calculations step by step. This allows you to examine the formula in greater detail and hopefully find the culprit.
- Press "Close" button to dismiss the dialog box.
There is also another way to debug formulas using the function key F9. F9 is especially useful if you have a feeling that a specific part of the formula is the issue, this makes it faster than the "Evaluate Formula" tool since you don't need to go through all calculations to find the issue..
- Enter Edit mode: Double-press with left mouse button on the cell or press F2 to enter Edit mode for the formula.
- Select part of the formula: Highlight the specific part of the formula you want to evaluate. You can select and evaluate any part of the formula that could work as a standalone formula.
- Press F9: This will calculate and display the result of just that selected portion.
- Evaluate step-by-step: You can select and evaluate different parts of the formula to see intermediate results.
- Check for errors: This allows you to pinpoint which part of a complex formula may be causing an error.
The image above shows cell reference B2:D4 converted to hard-coded value using the F9 key. The VSTACK function requires non-error values which is not the case in this example. We have found what is wrong with the formula.
Tips!
- View actual values: Selecting a cell reference and pressing F9 will show the actual values in those cells.
- Exit safely: Press Esc to exit Edit mode without changing the formula. Don't press Enter, as that would replace the formula part with the calculated value.
- Full recalculation: Pressing F9 outside of Edit mode will recalculate all formulas in the workbook.
Remember to be careful not to accidentally overwrite parts of your formula when using F9. Always exit with Esc rather than Enter to preserve the original formula. However, if you make a mistake overwriting the formula it is not the end of the world. You can “undo” the action by pressing keyboard shortcut keys CTRL + z or pressing the “Undo” button
4.5 Other errors
Floating-point arithmetic may give inaccurate results in Excel - Article
Floating-point errors are usually very small, often beyond the 15th decimal place, and in most cases don't affect calculations significantly.
5. VSTACK Function alternative
There is unfortunately no way to merge cell ranges in earlier Excel versions unless you are willing to manually merge the ranges or use a User Defined Function.
The image above shows how to manually merge two cell ranges, this technique works in all Excel versions as far as I know, however, you are required to enter the array as an array formula in order to show all values.
Here are the steps:
- Select cell range B8:D:13. The resulting array is three columns wide and contains six rows, make sure you select a cell range that fits your data.
- Type = (equal sign).
- Select with mouse the first cell range B2:D4.
- Type a + (plus sign).
- Select with mouse the second cell range F2:H4.
- Select F2:H4 in the formula.
- Press F9 to convert the cell range to constants.
- Repeat steps 6 and 7 using the first cell range B2:D4, the result looks like this:
- Select the last curly bracket of the first array, the plus sign and the first curly bracket of the second array:
- Press Delete to remove those characters.
- Type a semicolon ;
- Steps 12 to 14 show how to enter the array as an array formula.
Press and hold CTRL + SHIFT simultaneously. - Press Enter once.
- Release all keys.
The formula has now a leading and ending curly bracket, they appear automatically. Don't enter these characters yourself.
This article describes how to merge cell ranges using a User Defined Function: Combine cell ranges ignore blank cells (User Defined Function)
6. Extract unique distinct rows from multiple cell ranges
This example demonstrates how to extract unique distinct rows/records from multiple non-adjacent cell ranges. The image above shows one data set in B3:D5 and another in F3:H5.
The VSTACK function stacks these data sets on top of each other. The UNIQUE function the merges duplicate rows/records to one distinct row/record for each instance.
Formula in cell B9:
This formula is an Excel 365 dynamic array formula. It is entered as regular formula and it spills values to cells below and to the right of B9 as far as needed. A #SPILL! error is shown if the destination cells are not empty.
Explaining formula
Tip! You can follow the formula calculations step by step by using the "Evaluate" tool located on the Formula tab on the ribbon. This makes it easier to spot errors and understand the formula in greater detail. The following steps shows the calculations in great detail.
Step 1 - Join cell ranges
VSTACK(array1,[array2],...)
VSTACK(B3:D5, F3:H5)
returns {89, "Charles",... , 62}.
Step 2 - Extract unique distinct rows
The UNIQUE function is a new Excel 365 function that returns unique and unique distinct values/rows.
UNIQUE(array,[by_col],[exactly_once])
UNIQUE(VSTACK(B3:D5, F3:H5), FALSE, FALSE)
returns
{89, "Charles", ... , 62}
The bolded rows are duplicates, only one instance of those rows in the result.
7. VSTACK Function works with 3D ranges
The image above demonstrates how to use 3D ranges in the VSTACK function. You must have data in the same location on each worksheet for this to work.
What is a 3d range?
A 3d range is a cell reference that references multiple worksheets in only one cell reference. However, you need to know how to create a 3D reference which is easy and the data must be on the same cell range on each worksheet. The image above shows that data on worksheet 1, 2 and 3 are on the same location.
Dynamic array formula in cell B3:
Some of the data sets are smaller than others, this makes the formula get blank values as well from cell ranges that contain blanks. The FILTER function filters out blanks based on if each cell in column A in each sheet is empty.
7.1 How to enter the 3D range cell reference
This is entered as a regular formula, however, the 3D range needs to be explained in greater detail.
To create this reference: '1:3'!A2:C10 follow these steps:
- Double press with left mouse button on cell B3, the prompt appears.
- Type: =FILTER(VSTACK(
- Go to worksheet: 1
- Select cell range A2:C10
- Press and hold SHIFT key.
- Select the remaining worksheets, they are 2 and 3 in this example.
- Type the remaining part of the formula and repeat the above steps for the second 3D range.
- Press Enter.
7.2 Explaining formula
Tip! You can follow the formula calculations step by step by using the "Evaluate" tool located on the Formula tab on the ribbon. This makes it easier to spot errors and understand the formula in greater detail. The following steps shows the calculations in great detail.
Step 1 - Merge data from three diffrent worksheets vertically
VSTACK('1:3'!A2:C10)
becomes
VSTACK('1'!A2:C10,'2'!A2:C10,'3'!A2:C10)
returns
{89,"Charles",..., 0}
Step 2 - Remove empty rows
The FILTER function filters values based on a condition or criteria.
FILTER(array, include, [if_empty])
FILTER(VSTACK('1:3'!A2:C10),VSTACK('1:3'!A2:A10)<>"")
returns
{89,"Charles",...,34}
Useful resources
VSTACK function - Microsoft support
How to combine ranges / arrays in Excel with VSTACK & HSTACK functions
'VSTACK' function examples
This post explains how to lookup a value and return multiple values. No array formula required.
Question: I need to calculate how many hours a machine is utilized in a company with a night and day […]
Table of Contents Compare tables: Filter records occurring only in one table Compare two lists and filter unique values where […]
Functions in 'Array manipulation' category
The VSTACK function function is one of 11 functions in the 'Array manipulation' category.
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form