How to Use SUMIF in Merged Cells in Google Sheets

Published on

When you use the SUMIF function in merged cells in Google Sheets, it may return an incorrect sum if the cells in the range are merged while the sum range is not.

To correctly use SUMIF with merged cells, you can apply the following formula:

=SUMIF(ARRAYFORMULA(LOOKUP(ROW(range), IF(LEN(range), ROW(range)), range)), criterion, sum_range)

Formula Components

  • range – The range to filter based on the criterion.
  • criterion – The condition to filter the range.
  • sum_range – The range to sum based on the criterion.

Understanding Merged Cells in Google Sheets

Unlike database applications, Google Sheets allows merging cells, which you can find under the Format menu. However, merging cells can disrupt the structured nature of a dataset, making it difficult to use functions like SUMIF, FILTER, QUERY, and others.

If your dataset contains merged cells, standard SUMIF formulas may return incorrect results. Below is an example illustrating this issue and the correct workaround.

Example: SUMIF Returning Incorrect Results

Consider the following dataset:

SUMIF returning incorrect results due to merged cells in Google Sheets

If you use a regular SUMIF formula like:

=SUMIF(B2:B10, E3, C2:C10)

It may return 10 instead of 100 (10+20+30+40) for the criterion “POMEGRANATE” in cell E3.

If you unmerge the cells and structure the data as a proper table, the formula works correctly.

Example of a standard conditional SUMIF formula in Google Sheets

However, if you want to keep merged cells, you need to modify the formula.

Correct SUMIF Formula for Merged Cells

Replace the above formula with:

=SUMIF(ARRAYFORMULA(LOOKUP(ROW(B2:B10), IF(LEN(B2:B10), ROW(B2:B10)), B2:B10)), E3, C2:C10)

This version ensures that merged cells are properly accounted for.

Correct application of SUMIF in merged cells in Google Sheets

Formula Explanation

Since the range contains merged cells, we create a virtual range using:

ARRAYFORMULA(LOOKUP(ROW(B2:B10), IF(LEN(B2:B10), ROW(B2:B10)), B2:B10))

This formula fills empty merged cells with values from the preceding non-empty cell. The adjusted data looks like this:

Apple
Apple
Pomegranate
Pomegranate
Pomegranate
Pomegranate
Banana
Banana
Banana

The SUMIF function then correctly sums values based on the adjusted range.

Conclusion

Using this approach, you can correctly apply SUMIF to merged cells in Google Sheets without restructuring your dataset. This method ensures accurate results even when cells are merged.

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.

How to Get the Fastest Time for Each Person in Google Sheets

Whether you’re tracking race results, event times, or any other timed activities, finding the...

Highlight the Smallest N Values in a Column in Google Sheets

Want to visually spotlight the lowest values in your data? In this tutorial, you'll...

Sort Each Row Individually in Excel Using a LAMBDA Formula

Sorting rows in Excel typically refers to rearranging entire datasets based on values in...

Sort by Field Labels Using the SORT and XMATCH Combo in Excel

Want to sort your Excel data by column names instead of column positions? Learn...

More like this

How to Get the Fastest Time for Each Person in Google Sheets

Whether you’re tracking race results, event times, or any other timed activities, finding the...

Highlight the Smallest N Values in a Column in Google Sheets

Want to visually spotlight the lowest values in your data? In this tutorial, you'll...

How to Sort Pie Slices in Google Sheets

To sort pie slices in a pie chart, you need to sort the data...

2 COMMENTS

  1. The formula is terrific as I continue to inevitability deal with the team merging cells in the Google Sheets we share.

    I’m having trouble with one thing and am not sure how to revise the formula to address it.

    I have 24 columns, and I want a total of column I, which has dollar amounts based on the date in column T.

    =SUMIF(ArrayFormula(lookup(row(T2:T4416),if(len(T2:T4416),
    row(T2:T4416)),T2:T4416)),B2,I2:I4416)

    Cell B2 = 7/18/2022

    Columns
    Row # | Column I | Column T
    3089 | $2,000 | 7/18/2022
    3094 | $2,222 | (blank cell)

    If I enter a “-” in the blank cell, it calculates the formula to only $2,000, which is the total I’m after.

    Any suggestion on how to fix this is greatly appreciated!

    Many thanks!

    • Hi, LeSha T.,

      I assume you have merged cells in T2:T4416. Otherwise, you might have used a simple formula like this.

      =sumif(T2:T4416,B2,I2:I4416)

      Now, to solve the issue, you must understand the reason.

      In a vertically merged range, only the first cell contains the value. All other cells are blank.

      My formula fills the blank cells in cell range T2:T4416 with the dates from just above cells.

      While doing so, the formula fills all blank cells, whether within merged range or standalone.

      So as you have suggested, leaving a hyphen in the cell that you want to skip will solve the issue.

      But it’s not practical with several such cells. So do as follows.

      1. Select T2:T4416.
      2. Go to Format > Number > Plain Text (later, we will revert to date).
      3. Go to Edit > Find and Replace.
      4. In the “Find” field, input ^([\t]*)$ and put a hyphen character in the “Replace with” field.
      5. Check “Match case” and “Search using regular expressions” and Done.
      6. Then revert T2:T4416 to date.

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.