SUMIF and merged cells

jasonconlon

Board Regular
Joined
Mar 14, 2002
Messages
80
I'm trying to do a SUMIF, but the problem I have is that the criteria I am trying to match is in a merged cell - and only the rows corresponding with the top of that merged cell are considered a match.

For instance, imagine the following 3x7 table, where Apples is a merged cell from A2-to-A4, and Bananas is a merged cell from A5-to-A7:

Type Total
Red 100
Apples Green 150
Yellow 80
Green 60
Bananas Yellow 120
Pink 30

If I want to sum all the apples using =SUMIF(A2:A7,"Apples",C2:C7) then I only get a result of 100 (C2) instead of 330 (C2+C3+C4).

Without unmerging cells, is it possible to write a formula to match and sum these correctly?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Im not entirely sure I understand what your asking but if a cell contains Apple mixed with other text and you want to sum the values where the word Apple is contained anywhere in the cell then try using wildcards...

=SUMIF(A2:A7,"*Apples*",C2:C7)
 
Upvote 0
Sorry, all the spaces were of course ripped out by the HTML parsing.

Try this, ignoring the dashes which are there in place of spaces.

------------Type---Total
------------Red-----100
Apples----Green---150
------------Yellow---80
------------Green---60
Bananas--Yellow---120
------------Pink-----30

Remember, Apples is a merged (and vertically centred, in this depiction) cell from A2-to-A4, and Bananas is likewise from A5-to-A7...
 
Upvote 0
...but no, I must have confused you because in this scenario "Apples" is not combined with any other text -- just the cells A2-to-A4 have a merged format, which contains the text "Apples".


If this isn't possible, then can anyone suggest an elegant macro to run down that first column, copying text ("Apples") it finds there and pasting it into all "empty" cells until it runs into new text ("Bananas") and continuing the process - but remembering to check for adjacent data so it knows when to stop (i.e. row 8 contains no data and should halt the process)..?

That is, to get the data looking like:

------------Type---Total
Apples-----Red-----100
Apples----Green---150
Apples-----Yellow---80
Bananas--Green---60
Bananas--Yellow---120
Bananas---Pink-----30

so that I can use that SUMIF formula successfully...
 
Upvote 0
Not very fancy Im afraid but presuming you always have 3 cells merged then this may work. Someone may have a more elegant solution.
Book2
ABCDEF
1FruitTypeTotal
2AppleRed100Apple430
3Green150Banana210
4Yellow180
5BananaGreen60
6Yellow120
7Pink30
Sheet1
 
Upvote 0
If only it were that easy!

Unfortunately the real data could have any number of rows associated with the merged cell, and worse still that same named merge cell could pop up again later in the list (e.g. Apples = 3 rows, Bananas = 2 rows, Apples = 12 rows, Oranges = 1 row, etc...) - and all instances of Apples need to go into the total sum.

Interesting attempt, though. You've given me ideas for other conundrums I'm currently working on, but unfortunately not suitable for this particular problem. I really appreciate the effort!
 
Upvote 0
mmm Im not sure how that could be achieved with a formula. A macro should be able to do it though, so I'll have a look into it after dinner. :-D
 
Upvote 0
See if this works for you. If so, I will see if I can get this to work within a function. Run the RunMacro procedure which in turn runs the Test procedure. Place in a standard module.

Code:
Sub Test(Rng As Range, Criteria As String)
Dim Cell As Range, FirstAddress As String, MySum As Double
Dim c As Range

Set Cell = Rng.Find(Criteria, LookIn:=xlValues)

If Not Cell Is Nothing Then
        FirstAddress = Cell.Address
        Do
            If Cell.MergeCells = True Then
                For Each c In Cell.MergeArea
                MySum = MySum + c.Offset(0, 2).Value 'this means 2 columns to the right of the merged cell
                Next c
            Else
                MySum = MySum + Cell.Offset(0, 2).Value 'this means 2 columns to the right of the merged cell
            End If
            Set Cell = Rng.FindNext(Cell)
            If Cell Is Nothing Then Exit Do
        Loop While Cell.Address <> FirstAddress
End If

MsgBox MySum

End Sub


Sub RunMacro()
Dim MyRng As Range
Set MyRng = Range("A1:A10")  'Change this to match your range of the merged cells column
Call Test(MyRng, "Apple") 'looking for apples
End Sub
 
Upvote 0
Yep, that does the right thing, and gives a correct result. If you can turn that into a formula, it would be great!

Failing that, I would need help to develop a macro that:
1) finds the first merged cell anywhere in the worksheet
2) takes note of the range of cells covered by that merged cell
3) and copies the value from the first cell in that merged range to all the other cells in that range (and subsequently unmerging the cells)
4) repeats the 1-to-3 cycle until there are no more merged cells to be found
...so that I don't have to deal with the "merged cell" problem.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top