Function Problems

Excel Formula to Return the First Heading with X or Z

Steve KSteve K Nov 12, 2026 868 views

Question details

Return the first column heading for a column where the data contains either x or z.

Excel Formula to Return the First Heading Containing X or Z
Product
Microsoft Excel
Device & OS
not provided
Scenario
The worksheet has headings in A1:F1 and data in A2:F5, and the user needs the first matching heading when scanning columns from left to right.
Observed behavior
The desired result is the heading of the leftmost column containing at least one x or z, such as 4H.
Before you start

Confirm that the headings and data ranges have the same number of columns. The formulas below look for the exact lowercase text values x and z; adjust the criteria if your worksheet uses different values.

Solution 1Recommended

Use a dynamic-array formula in Excel 365

Use this method when your version of Excel supports LET and TOCOL and you want one formula without helper cells.

The formula checks every data cell, keeps the corresponding column heading when it finds x or z, flattens the results from left to right, and returns the first heading found.

1
Select the result cell

Click the cell where you want the matching heading to appear, such as H1.

2
Enter the formula

Type or paste =LET(result,TOCOL(IF((A2:F5="x")+(A2:F5="z"),A1:F1,""),2,TRUE),INDEX(result,1)) and press Enter.

3
Adjust the worksheet ranges

Replace A1:F1 with your heading row and A2:F5 with the corresponding data range, keeping both ranges aligned by column.

Use a dynamic-array formula in Excel 365
Result: The formula returns the heading from the first leftmost column that contains x or z.
WPS Spreadsheets formula method

Find the First Matching Column Heading in WPS Spreadsheets

WPS Spreadsheets can identify the first heading whose column contains x or z with familiar worksheet formulas. Use COUNTIF in a helper row, then combine INDEX and MATCH to return the leftmost matching heading.

  1. 1. Open the worksheet in WPS Spreadsheets: Open the XLSX or XLS file, then confirm that the headings are in one row and the data is directly below them.
  2. 2. Create a count for the first column: Click a blank cell below the first data column, such as A6, and enter =COUNTIF(A2:A5,"x")+COUNTIF(A2:A5,"z").
  3. 3. Fill the count across all columns: Select A6 and drag its fill handle right through the final data column, such as F6.
  4. 4. Return the leftmost matching heading: Click the output cell and enter =INDEX(A1:F1,1,MATCH(TRUE,A6:F6>0,0)), then press Enter.
Use COUNTIF, INDEX, and MATCH to locate the first matching column heading.Review helper-row counts to verify which columns contain x or z.Open and edit Microsoft Excel XLSX and XLS workbooks with compatible spreadsheet formatting.Use a familiar spreadsheet interface for formula entry and fill-handle copying.
microsoft office alternative - wps office

Frequently Asked Questions

What does the formula return if more than one column contains x or z?

It returns the heading for the first matching column from left to right. For example, if columns B and E contain a match, the formula returns the heading in B1.

Can I search for uppercase X and Z as well?

COUNTIF is not case-sensitive, so x and X are treated as the same text. If you need case-sensitive matching, use a different formula approach based on EXACT.

Why does the dynamic-array formula return an error?

Check that your Excel version supports LET and TOCOL, and verify that the heading range and data range cover the same columns. For older Excel, use the COUNTIF helper-row method.

How do I search for different values instead of x and z?

Replace "x" and "z" in the formula with your required criteria. For example, use "yes" and "pending" to find the first column containing either of those values.