How to Sum a Column by Its Header Name in Excel
By ExcelReady TeamUpdated 6 min read
The short answer
To sum a column by its header, find the column with a lookup and wrap it in SUM. In Microsoft 365 or Excel 2021, use =SUM(XLOOKUP(G2,B1:E1,B2:E6)). In any version, use =SUM(INDEX(B2:E6,0,MATCH(G2,B1:E1,0))). Change the header name in G2 and the total moves to that column.
Key takeaways
- XLOOKUP can return a whole column: =SUM(XLOOKUP(G2,B1:E1,B2:E6)) adds the column whose header matches G2.
- In older Excel, INDEX with a row number of 0 returns the whole column that MATCH finds.
- The header range and the data range must start and end in the same columns, or you sum the wrong one.
- SUMPRODUCT((B1:E1=G2)*B2:E6) also works in every version and returns 0 when no header matches.
On this page+
To sum a column by its header in Excel, you let a lookup function find the column and then wrap it in SUM. Type a header name such as "Mar" in a cell, and the total switches to that column with no edits to the formula. This guide is for anyone who builds a summary or dashboard where the reader picks the month, region or product to total.
Why sum a column by its header name?#
A formula that finds its column by name keeps working when the layout changes. =SUM(D2:D6) always adds column D. If someone inserts a column or the reader wants April instead, you have to edit the formula by hand.
A header-based formula reads the column name from a cell, so one formula can answer "total for March", "total for April" and so on. It is the same idea as a two-way lookup that picks a row and a column, except that you keep every row and add them up.
All the examples use this monthly sales table in A1:E6, with the month to total in G2:
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Store | Jan | Feb | Mar | Apr |
| 2 | Leeds | 1,200 | 950 | 1,100 | 1,300 |
| 3 | York | 800 | 1,150 | 900 | 1,000 |
| 4 | Hull | 1,500 | 1,250 | 1,400 | 1,600 |
| 5 | Bath | 700 | 850 | 650 | 900 |
| 6 | Derby | 1,000 | 1,100 | 1,200 | 950 |
How to sum a column by header with XLOOKUP#
In Microsoft 365 or Excel 2021 and later, XLOOKUP can return a whole column, so SUM around XLOOKUP does the job:
=SUM(XLOOKUP(G2,B1:E1,B2:E6))Here is what each part does:
- G2 is what to find: the header name, "Mar".
- B1:E1 is where to look: the header row.
- B2:E6 is what to return. It has five rows, so XLOOKUP returns all five values from the matching column.
- SUM adds those values.
With "Mar" in G2, the result is 5,250 (1,100 + 900 + 1,400 + 650 + 1,200). Type "Feb" in G2 and it changes to 5,300. XLOOKUP looks for an exact match by default and is not case-sensitive, so "mar" works too. The XLOOKUP function guide lists the optional arguments.
How to sum a column by header with INDEX and MATCH#
INDEX and MATCH do the same job in every version of Excel. MATCH finds the column number and INDEX returns that column:
=SUM(INDEX(B2:E6,0,MATCH(G2,B1:E1,0)))MATCH(G2,B1:E1,0)returns 3, because "Mar" is the third header in B1:E1. The final 0 asks for an exact match. See the MATCH function guide for the other match types.INDEX(B2:E6,0,3)returns the third column of the data. The 0 as the row number is the key: it means "all rows", not one cell. The INDEX function guide explains this.- SUM adds the column: 5,250 again.
This is the INDEX MATCH pattern with the row left open. If you have used INDEX MATCH MATCH for two-way lookups, you replace the row MATCH with 0. For a refresher on why MATCH returns a position, read how the MATCH function works.
Use the same lookup with AVERAGE, MAX or SUMIFS#
The lookup returns a range, so any function that accepts a range can use it. SUM is only the most common choice:
| Question | Formula | Result |
|---|---|---|
| Average for the month in G2 (Mar) | =AVERAGE(XLOOKUP(G2,B1:E1,B2:E6)) | 1,050 |
| Best store result in April | =MAX(INDEX(B2:E6,0,MATCH("Apr",B1:E1,0))) | 1,600 |
| March total for stores that sold 1,000+ in January | =SUMIFS(XLOOKUP(G2,B1:E1,B2:E6),B2:B6,">=1000") | 3,700 |
The SUMIFS version adds Leeds, Hull and Derby: 1,100 + 1,400 + 1,200 = 3,700. The criteria range (B2:B6) must have the same number of rows as the data range, or SUMIFS returns #VALUE!.
You can also flip the idea and total a row by its label. =SUM(XLOOKUP("Hull",A2:A6,B2:E6)) returns Hull's whole row and adds it: 5,750. The guide to SUM and AutoSum covers the basics of SUM itself.
The SUMPRODUCT alternative#
SUMPRODUCT can sum by header without a lookup function. It compares every header to G2 and multiplies the result by the data:
=SUMPRODUCT((B1:E1=G2)*B2:E6)B1:E1=G2 gives TRUE only under "Mar". TRUE counts as 1 and FALSE as 0, so only the March column survives the multiplication. The result is 5,250.
Two differences are worth knowing:
- No match gives 0, not #N/A. That is quieter, but it can hide a typo in G2.
- Duplicate headers are added together. If two columns are both called "Mar", SUMPRODUCT adds both, while XLOOKUP and MATCH use only the first one.
Why does the sum by header return #N/A or the wrong total?#
Most problems come from the header text or from ranges that don't line up. Check these:
- #N/A: the header doesn't exist. "March" won't match "Mar", and "Mar " with a trailing space won't either. Clean the input with TRIM, or use a drop-down list for G2 so readers can only pick real headers.
- Dates in the header row. If B1 holds a real date shown as "Jan", the cell contains a number, not the text "Jan". Match it with a real date in G2, or use text headers.
- Misaligned ranges. The header range and the data range must cover the same columns. B1:E1 with C2:F6 returns the column one place to the right of the one you asked for.
- The label column inside the data. If the data range starts at column A, MATCH counts from A and the position is off by one. Start both ranges in column B.
- A blank result instead of a total. With XLOOKUP you can set a fallback:
=SUM(XLOOKUP(G2,B1:E1,B2:E6,0))returns 0 when the header is missing.
To show which header a value sits under, rather than a total, see how to return the column header for a value.
Practise it#
The formula on this page combines three skills: a lookup, a range that returns a whole column, and SUM. Practise XLOOKUP in a live spreadsheet, then practise INDEX and MATCH for files that must work in older Excel. If SUM itself is new to you, start with the SUM and AutoSum lesson. Each lesson checks your formula as soon as you press Enter.
Try it yourself
This is a live spreadsheet. Type the formula in the green cell and press Enter.
Cell G2 holds a month name. In H2, add up the column whose header matches G2: use SUM around XLOOKUP, with B1:E1 as the headers and B2:E6 as the data.
| A | B | C | D | E | F | G | H | I | |
|---|---|---|---|---|---|---|---|---|---|
| 1 | Apr | Month | |||||||
| 2 | Mar | ||||||||
| 3 | |||||||||
| 4 | |||||||||
| 5 | |||||||||
| 6 | |||||||||
| 7 | |||||||||
| 8 | |||||||||
| 9 |
Tip: XLOOKUP looks for G2 in the header row and returns the matching column of B2:E6. SUM adds it up.
Practise this in a lesson
Frequently asked questions
How do I sum a column based on the header name in Excel?
Use =SUM(XLOOKUP(G2,B1:E1,B2:E6)), where G2 holds the header name, B1:E1 is the header row and B2:E6 is the data. In older Excel, use =SUM(INDEX(B2:E6,0,MATCH(G2,B1:E1,0))).
Can XLOOKUP return a whole column?
Yes. When the lookup array is a row of headers and the return array has several rows, XLOOKUP returns the full matching column. You can wrap it in SUM, AVERAGE, MAX or any function that takes a range.
Why does INDEX use 0 as the row number?
A row number of 0 tells INDEX to return every row in the chosen column instead of a single cell. Combined with MATCH for the column number, it gives you the whole column to add up.
How do I sum a column by header and a condition?
Use the lookup as the sum range of SUMIFS, for example =SUMIFS(XLOOKUP(G2,B1:E1,B2:E6),B2:B6,">=1000"). The criteria range must have the same number of rows as the data.
Why does my sum by header return #N/A?
The header in your input cell doesn't match any header exactly. Look for extra spaces, a typo, or dates in the header row that are real dates while your input is text.