Problem using countif and offset when data is contained on two rows (see example)

ShawnLerner

New Member
Joined
Nov 3, 2016
Messages
10
This is a simplified representation of the problem:

How do I write a formula that will count the number of green apples from a dataset like the one below where the odd number rows have fruit type and the even number row right below it has the corresponding fruit colour?


A
1 Apple
2 Green
3 Apple
4 Blue
5 Peach
6 Green
7 Apple
8 Green


In this example the value I want to get is 2. (A1=Apple, A2=Green + A7=Apple, A8=Green)


I think I should be able to do this using Countif and Offset but can't for the life of me figure out how. Any help would be most appreciated!
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

ShawnLerner

New Member
Joined
Nov 3, 2016
Messages
10
Thanks but both
=COUNTIFS(A1:A8,"Apple",A2:A9,"Green")
and

=SUMPRODUCT(--($A$1:$A$7="apple"),--($A$2:$A$8="green"))

returned 1 not 2. Any other suggestions would be appreciated.

 

Aladin Akyurek

MrExcel MVP
Joined
Feb 14, 2002
Messages
85,179
Thanks but both
=COUNTIFS(A1:A8,"Apple",A2:A9,"Green")
and

=SUMPRODUCT(--($A$1:$A$7="apple"),--($A$2:$A$8="green"))

returned 1 not 2. Any other suggestions would be appreciated.

The count should be 2. What happens if we test the following?

=SUMPRODUCT(--(TRIM($A$1:$A$7)="apple"),--(TRIM($A$2:$A$8)="green"))
 

Aladin Akyurek

MrExcel MVP
Joined
Feb 14, 2002
Messages
85,179
That did the trick. Thank you very much!
You are welcome. This formula shows that you have errand spaces around the entries in your data. You might want to consider cleaning your data and revert back to a formula without TRIM calls.
 

AliGW

.
Joined
Mar 9, 2014
Messages
3,628
Thanks but both
=COUNTIFS(A1:A8,"Apple",A2:A9,"Green")
and

=SUMPRODUCT(--($A$1:$A$7="apple"),--($A$2:$A$8="green"))

returned 1 not 2. Any other suggestions would be appreciated.

My suggestion returned 2 here using the data you provided.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,274
Messages
5,485,783
Members
407,515
Latest member
franjey

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top