HSTACK Function¶
The HSTACK function in Excel is used to horizontally stack values into a single array. It takes one or more arguments and places each one side-by-side (in columns) to form a new horizontal array. This is particularly useful for combining values from different ranges or constants into a single row-oriented structure.
Key Features of HSTACK:¶
- Combines values from multiple sources into a single horizontal array.
- Works with both scalar values and arrays.
- Supports dynamic arrays and spills results into adjacent columns.
Syntax:¶
- array1, array2, …: One or more arrays or values to be stacked horizontally. Scalars are treated as 1×1 arrays.
How HSTACK Works:¶
- Each input is evaluated and converted into an array if necessary.
- Arrays are aligned row-wise and concatenated column-wise.
- The final result is a single horizontal array where each input is placed to the right of the previous one.
- If arrays have differing row counts, Excel pads shorter arrays with blanks.
Examples:¶
1. Combine Scalar Strings:¶
Result:
2. Combine Ranges Horizontally:¶
Assume:
- A1:A2 contains 1, 2
- B1:B2 contains 3, 4
Result:
3. Stack Mixed Scalars and Arrays:¶
Result:
Note how "C" is broadcasted across two rows to match the height of {"D";"E"}.
Notes:¶
HSTACKis available in Excel 365 and Excel 2021.- It automatically resizes the result (spills) to fit the array.
- If used with incompatible types (e.g., arrays with mismatched structures), the function may return a
#SPILL!error.
Applications:¶
- Dynamic Table Creation: Combine multiple columns of data into a single structured array.
- Data Reshaping: Rearrange vertical or mixed arrays into horizontal format.
- Report Generation: Build formatted output ranges by joining different data sources.
Tip: Use
VSTACKif you want to stack values vertically instead of horizontally.