Google-sheets – How to reference the values of merged cells in formulas

formulasgoogle sheets

Say I have the following table (with cells A1 & A2 merged together to have the common value of 500):

 | Col A | Col B | Col C  |
 |-------|-------|--------|
1| 500   | 100   | =A1+B1 |
2|       | 200   | =A2+B2 |

The Value of C1 is correctly populated to 600 while the value of C2 is populated as 200 instead of the expected 700. This is because even though the cell A2 is merged with A1 which has a value of 500, the reference to A2 returns 0.

Is there any way I reference the value of the merged cell in a formula?

For instance, is there a formula that when passed a cell reference A2 returns either the value of A1 or tells me that A2 has been merged with a top left location of A1?

Are there any workarounds which are more practical than copying and pasting (and then keeping in sync) the values of A1 and A2?

Best Answer

more practical than copying and pasting (and then keeping in sync) the values of A1 and A2?

If you want A2 to have the same value as A1, do not merge them but enter =A1 in A2. This takes care of keeping them as sync; only A1 should be updated.

Spreadsheet formulas have to refer to merged cells by their top leftmost entry. They cannot detect that cells were merged. Thus, it is not advisable for cells with data that will be used elsewhere. Cell merge is fine for text, e.g. to have a header of a table spanning several columns.

Google Apps Script can detect merge with isPartOfMerge method, and find the range in which the cell is merged with getMergedRanges method. I wrote a custom function that retrieves the value of a cell, when cell address is passed in as a string, e.g., =cellVal("A3").

function cellVal(cellAddress) {
  var cell = SpreadsheetApp.getActiveSheet().getRange(cellAddress);
  return (cell.isPartOfMerge() ? cell.getMergedRanges()[0].getCell(1, 1) : cell).getValue();
}

This is only meant as a demonstration; I do not recommend using custom functions for retrieving data from a cell.