Create an Excel Formula to Count Matching Types and Codes Across Columns
Question details
Create a formula that evaluates Type and Code values in two columns and returns a cross-tabulated count for each unique Type and Code combination.

- Product
- Microsoft Excel for Microsoft 365
- Device & OS
- not provided
- Scenario
- The user wants to summarize source data by placing unique types across columns, unique codes down rows, and counting the matching records at each intersection.
- Observed behavior
- The desired result is a dynamic matrix that updates its unique headers and matching counts when the source data changes.
Make sure the Type and Code source ranges contain the same number of rows and have matching records on each row. For the dynamic-array formula, use Excel for Microsoft 365 and define the source columns as named ranges or replace them with table column references.
Use a Microsoft 365 dynamic-array formula with named Type and Code ranges
Use this method when you need one formula to generate unique row and column headers plus the count of every Type and Code pair.
This formula uses LET, UNIQUE, SORT, TOROW, VSTACK, HSTACK, and MMULT to build a complete cross-tabulation. The named ranges type and code must refer to equally sized source columns.
Select the Type data column, then go to Formulas > Define Name and create the name type. Repeat for the Code data column and create the name code.
Click an empty cell where the cross-tabulation should begin. Leave enough empty cells to the right and below for the spilled results.
Enter =LET(types,TOROW(SORT(UNIQUE(type))),codes,SORT(UNIQUE(code)),VSTACK(HSTACK("",types),HSTACK(codes,MMULT(--(TRANSPOSE(TOROW(codes)=code)),--(type=types))))) and press Enter.
Verify that the top row contains unique Type values, the first column contains unique Code values, and each intersecting cell shows the count of matching source rows.

Use Excel table references instead of named ranges
Use this variation when your source data is stored in an Excel table and you prefer structured references that expand automatically as new rows are added.
Create a Type and Code Count Matrix in WPS Spreadsheet
WPS Spreadsheet can create the same two-criteria summary with unique row and column headers and a COUNTIFS formula. This approach is useful when you want a clear, editable cross-tabulation without relying on a complex dynamic-array formula.
- 1. Place unique Type values across the top: Copy the Type column to an empty area, then use Data > Remove Duplicates. Copy the resulting unique values horizontally into the summary header row.
- 2. Place unique Code values down the first column: Copy the Code column to the first column of the summary area, then select Data > Remove Duplicates to retain one instance of each code.
- 3. Enter a COUNTIFS formula at the first intersection: In the first count cell, enter =COUNTIFS($A$2:$A$100,$D2,$B$2:$B$100,E$1), replacing the first source range with Type, the second source range with Code, $D2 with the current Code row label, and E$1 with the current Type header.
- 4. Fill the formula across and down: Select the first result cell and drag the fill handle across all Type headers and down all Code rows to calculate every combination.

Frequently Asked Questions
What does the MMULT part of the Excel formula calculate?
MMULT multiplies two TRUE/FALSE comparison arrays after they are converted to 1 and 0. The resulting matrix contains the number of records for each Code row and Type column combination.
Can I replace the named ranges with normal cell ranges?
Yes. Replace type and code with matching ranges, such as $A$2:$A$100 and $B$2:$B$100. Both ranges must start and end on corresponding rows.
Why does the formula return a #SPILL! error?
A #SPILL! error means cells in the intended output range are not empty. Clear the blocking cells or move the formula to an area with sufficient empty rows and columns.
How can I count only matching records instead of creating a full matrix?
Use COUNTIFS with the two criteria directly, such as =COUNTIFS(A:A,"Type A",B:B,"Code 100"). Replace the criteria with cell references when needed.
Does this formula work in older Excel versions?
No. LET, UNIQUE, VSTACK, HSTACK, and dynamic spilling require Microsoft 365 or a version that supports these functions. In older versions, create unique headers manually and use COUNTIFS for each intersection.




