Counif function

ryan8200

Active Member
Joined
Aug 21, 2011
Messages
357
How to calculate the total fruits consumed by 3 students within 5 days ? Please take note each student can consume more than 1 per day ?

Student 1
Student 2
Student 3
Day 1
apple / kiwi
orange
apple
Day 2
orange
honey dew
apple / kiwi
Day 3
kiwi
apple
orange
Day 4
honey dew / kiwi
kiwi
kiwi
Day 5
apple
kiwi
apple

<tbody>
</tbody>


Final Output


Apple : 6
Kiwi : 7
Orange : 3
Honey Dew: 2
 
This is the output I get
Apple
7
Kiwi
9
Orange
Honey Dew

<tbody>
</tbody>


Why the value for orange & honey dew is 0 ? Does this mean I have to ensure all fruits is repeated before I use your formula?

Try this formula:

Code:
In B9

=SUMPRODUCT((LEN(" "&TRIM(SUBSTITUTE($A$2:$C$6,"/"," / "))&" /")-LEN(SUBSTITUTE(" "&UPPER(TRIM(SUBSTITUTE($A$2:$C$6,"/"," / ")))&" /"," "&TRIM(UPPER($A9))&" /","")))/
LEN(" "&TRIM($A9)&" /"))

PS: you have extra spaces in some cells of the range A9:A12 in your workbook.

Markmzz
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this formula:

Code:
In B9

=SUMPRODUCT((LEN(" "&TRIM(SUBSTITUTE($A$2:$C$6,"/"," / "))&" /")-LEN(SUBSTITUTE(" "&UPPER(TRIM(SUBSTITUTE($A$2:$C$6,"/"," / ")))&" /"," "&TRIM(UPPER($A9))&" /","")))/
LEN(" "&TRIM($A9)&" /"))

PS: you have extra spaces in some cells of the range A9:A12 in your workbook.

Markmzz

It works !!! Mark

You are my hero :)

Have to use TRIM to remove the leading & trailing spaces
 
Upvote 0
Try this formula:

Code:
In B9

=SUMPRODUCT((LEN(" "&TRIM(SUBSTITUTE($A$2:$C$6,"/"," / "))&" /")-LEN(SUBSTITUTE(" "&UPPER(TRIM(SUBSTITUTE($A$2:$C$6,"/"," / ")))&" /"," "&TRIM(UPPER($A9))&" /","")))/
LEN(" "&TRIM($A9)&" /"))

PS: you have extra spaces in some cells of the range A9:A12 in your workbook.

Markmzz

Hi Mark,

What has caused the extra spaces ? How to avoid the same problem in future ?
 
Upvote 0
Hi Mark,

What has caused the extra spaces ? How to avoid the same problem in future ?

Hi Ryan,

In your workbook, in cells A11, you have Orange with a space in the final of the word and, in the cell A12, you have Honey Dew with a space in the final of the last word.

With the last formula below, you don't need to worry about the problem (the TRIM function remove the extra spaces for you).

Code:
In B9

=SUMPRODUCT((LEN(" "&TRIM(SUBSTITUTE($A$2:$C$6,"/"," / "))&" /")-LEN(SUBSTITUTE(" "&UPPER(TRIM(SUBSTITUTE($A$2:$C$6,"/"," / ")))&" /"," "&[COLOR="#FF0000"]TRIM[/COLOR](UPPER($A9))&" /","")))/
LEN(" "&[COLOR="#FF0000"][/COLOR][COLOR="#FF0000"]TRIM[/COLOR]($A9)&" /"))

And copy down.

Markmzz
 
Upvote 0
Hi Ryan,

In your workbook, in cells A11, you have Orange with a space in the final of the word and, in the cell A12, you have Honey Dew with a space in the final of the last word.

With the last formula below, you don't need to worry about the problem (the TRIM function remove the extra spaces for you).

Code:
In B9

=SUMPRODUCT((LEN(" "&TRIM(SUBSTITUTE($A$2:$C$6,"/"," / "))&" /")-LEN(SUBSTITUTE(" "&UPPER(TRIM(SUBSTITUTE($A$2:$C$6,"/"," / ")))&" /"," "&[COLOR=#ff0000]TRIM[/COLOR](UPPER($A9))&" /","")))/
LEN(" "&[COLOR=#ff0000]TRIM[/COLOR]($A9)&" /"))

And copy down.

Markmzz


Markmzz[/QUOTE]


Hi Mark,

I'm still study on your formula.

Actually, it takes how many days for you to figure out the solutions ? Also, how do you come out with the solutions ? It's very compliacted.

If remove " " & " /" after the TRIM function, myapple will be counted as apple . This is what I have discovered beneath your formula.
 
Upvote 0
Markmzz

Hi Mark,

I'm still study on your formula.

Actually, it takes how many days for you to figure out the solutions ? Also, how do you come out with the solutions ? It's very compliacted.

If remove " " & " /" after the TRIM function, myapple will be counted as apple . This is what I have discovered beneath your formula.

Hi Ryan,

Use the evaluate formula feature of Excel to better understand how it works.

More information for evaluate formula feature :

Evaluate a nested formula one step at a time - Excel - Office.com

Markmzz
 
Upvote 0

Forum statistics

Threads
1,216,118
Messages
6,128,939
Members
449,480
Latest member
yesitisasport

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