HomeGoogle DocsSpreadsheetXLOOKUP in Merged Cells in Google Sheets

XLOOKUP in Merged Cells in Google Sheets

Published on

In Google Sheets, merging cells is not a good idea if you intend to manipulate your data. However, there might be reasons why you chose to merge cells in your dataset.

Suppose you need to look up a value using the XLOOKUP function in merged cells. How can this be achieved in Google Sheets?

To do this task, we will utilize a nested XLOOKUP formula. The inner XLOOKUP function will assist the outer XLOOKUP in searching for the key and returning the desired value.

Sample Data

The sample data is located in cells A1:C10. In this dataset, the first column contains employee names, the second column contains month names, and the third column contains their corresponding monthly salaries.

Sample Data for XLOOKUP (Lookup Range is merged)

Please note that the employee names are merged cells. We will use an XLOOKUP formula to search for an employee’s name within this merged column and retrieve their salary (earliest or most recent) from the third column.

XLOOKUP Formula for Merged Cells

Cell D2 contains the employee name “Ben,” which serves as our search key.

The following XLOOKUP formula searches for the key in cell D2 within the merged cells in the first column and returns his most recent salary from the third column.

=ArrayFormula(LET(
      l_r, A2:A10, 
      r_r, C2:C10, 
      s_k, D2, 
      XLOOKUP(
          s_k, 
          XLOOKUP(ROW(l_r), IF(LEN(l_r), ROW(l_r)), l_r, ,-1), 
          r_r, ,0, -1
      )
))

The formula searches from bottom to top and returns the most recent salary. To achieve this, we’ve specified the search mode as -1 (highlighted in the formula).

Example of XLOOKUP in Merged Cells in Google Sheets

If you want his earliest salary (search from top to bottom), use 1 as the search mode.

The formula follows the syntax:

XLOOKUP(search_key, lookup_range, result_range, [missing_value], [match_mode], [search_mode])

where:

  • search_key: D2
  • lookup_range: A2:A10
  • result_range: C2:C10

You only need to specify these references and the highlighted search mode, and the formula will take care of the rest.

Now, you might be wondering how XLOOKUP can look up in the merged column and what workaround we have followed.

Formula Explanation

If you have vertically merged cells and enter a value in them, the value will appear in the topmost cell of the group. In our example, cells A2:A4, A5:A7, and A8:A10 are merged. Therefore, the employee names Ben, Mike, and Rose are located in cells A4, A5, and A8 only.

All other cells within the range A2:A10 are blank. Consequently, we need to fill the blank cells with the corresponding employee names. We will achieve this virtually, and that’s the logic behind XLOOKUP in merged cells.

In our formula, the following part accomplishes this task:

XLOOKUP(ROW(l_r), IF(LEN(l_r), ROW(l_r)), l_r, ,-1)

This part (inner XLOOKUP) acts as the lookup range in the outer XLOOKUP. But what does this formula do?

This XLOOKUP formula searches for the row numbers of the actual lookup range [ROW(l_r)] in the row numbers corresponding to employee names [IF(LEN(l_r), ROW(l_r))] and returns the corresponding employee names [ l_r ] in the matching rows.

The key aspect here is the match mode. It’s set to -1, which indicates an exact match or the next value that is lower than the search key. This effectively fills the column.

Please refer to the illustration:

Virtual Lookup Range for XLOOKUP

We utilize this resulting column as the lookup range in our outer XLOOKUP.

In summary, XLOOKUP itself is not capable of searching for keys in merged cells in Google Sheets. Therefore, we need to use another formula to generate a virtual array with values filled in merged cells. For this purpose, we have employed a second XLOOKUP.

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

More like this

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to the...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.