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

#### ShawnLerner

##### New Member
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!

### 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

#### AliGW

##### .
Try this:

=COUNTIFS(A1:A8,"Apple",A2:A9,"Green")

##### MrExcel MVP
Like this:

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

#### ShawnLerner

##### New Member
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.

##### MrExcel MVP
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"))

#### ShawnLerner

##### New Member
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"))
That did the trick. Thank you very much!

##### MrExcel MVP
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

##### .
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.

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...