Why does this COUNTIF formula give 2? - excel-formula

I have this COUNTIF formula:
=COUNTIF(A1:A20,A1:A20&"")
When I highlighted the formula and hit F9, I got the following array:
{1;2;2;1;1;1;1;1;1;2;2;2;1;2;4;4;4;4;1;1}
The formula by itself gives 2. I wonder from where the 2 come.
GDPXT01
GDPXT02
GDPXT02
GDPXT03
GDPXT04
PXQZW01
PXQZW02
PXQZW03
PXQZW04
OCCMD2
OCCMD2
DSOMR1
ITY
DSOMR1
PXQZW05
PXQZW05
PXQZW05
PXQZW05
BKXMA01
BKXMA02

It is important to know what is data in A1:A20, but this is result of your function, it is array! For example, for this data-set below you will get same result.
1,
2,
2,
3,
4,
5,
6,
7,
8,
9,
9,
10,
11,
10,
12,
12,
12,
12,
13,
14

We don't have clear understanding about your question unless see your excel sheet.
So I made some points that will helps for your understanding about COUNTIF function. COUNTIF, is used to count cells that match criteria.
=COUNTIF (range, criteria)
range - The range of cells to count.
criteria - The criteria that controls which cells should be counted.
Some Important Notes:
COUNTIF is a function to count cells that meet a single criteria.
COUNTIF can be used to count cells with dates, numbers, and text that
match specific criteria. The COUNTIF function supports logical
operators (>,<,<>,=) and wildcards (*,?) for partial matching.
Some Examples:
=COUNTIF(D5:D12,">100")
=COUNTIF(B5:B12,"jim")
Hope this might helps :)

Because you are using COUNTIF wrong. As per the documentation:
COUNTIF(range, criteria)
range (required) - The group of cells you want to count
criteria (required) - A number, expression, cell reference, or text string that determines which cells will be counted.
This tells you that Criteria accepts only one value, and not a range/array.
If you use the "Evaluate Formula" tool, you will see that your criteria (A1:A20&"") matches the current row. On row 1, it is treated as A1&"". On row 2, it is treated as A2&"". On row 15, it is treated as A15&"". You can also put identical copies of your formula in every row, from 1 to 20, to see this.
As such - on rows 2, 3, 10, 11, 12, and 14 the result will be 2. On rows 1, 4, 5, 6, 7, 8, 9, 13, 19, and 20 the result will be 1. And, on rows 15, 16, 17, and 18, the result will be 4.
As a bonus, rows 21+ will give a result of 0

Related

Sorting by value pulled from another cell

I am trying to isolate a sort by a number found at the end of a cell's value (i.e "APG-1710-082521-A4" and returning the value "4")
This is the formula that I am using:
=RIGHT(A2,LEN(A2)-SEARCH("$",SUBSTITUTE(A2,"A","$",LEN(A2)-LEN(SUBSTITUTE(A2,"A","")))))
This formula works for getting the number isolated in a new column, but when I sort that column, it only sorts by the first digit
Example:
1,
1,
1,
1,
12,
14,
15,
15,
15,
17,
2,
2,
2,
225,
What can I do to make sure that the whole number/cell value is being read?
The key to make your own formula work is given by #BigBen, but your formula is rather lenghty. Here is a formula that will extract the number from the right of your string:
=-LOOKUP(1,-RIGHT(A1,ROW($1:$99)))
If you must make this dynamic (length-wise) you can both try (depending on your version of Excel):
=-LOOKUP(1,-RIGHT(A1,ROW($A1:INDEX($A:$A,LEN(A1)))))
Or:
=-LOOKUP(1,-RIGHT(A1,SEQUENCE(LEN(A1))))

How to create a formula that searches multiple pieces of information within a cell

I am trying to create conditional formatting based on the results of a "compatibility" chart. I would like to be able to reference a cell at the top of the spreadsheet, for example "A1". For instance IF A1 = 1, 5, or 7, and the second reference cell (for example B1) is 1, 5, or 7, then I want the current cell to return "Natural" and be highlighted green. If "B1" is 2,3,or 9, then I want the current formula to return "Compatible". If "B1 is 4 or 6, I want it to return "Challenge". If "B1" is 8, I want the formula to return "Neutral". In addition to this, I would like to know how to add other conditions to "A1" in addition to the ones above. For instance: If "A1" is 2, 4, 8 followed by the conditions, and again for if "A1" is 3,6,9, followed by the conditions. If the result is "Natural", I want the cell to be highlighted Green, if "Compatible", I want a yellow highlight, and a red highlight for "Challenge".
Would it be best to use a VLOOKUP table?
Here are the tables I want to reference. I initially wanted to use Table 2, but figured it would be easier to use Table 1
compatibility excel tables

How can I redistribute a range of values without duplicates between 1 and 100?

I have a list of values between 1 and 100, essentially a sort of ranking that occassionally skips a few numbers (for example, the first ten values are 2, 6, 6, 10, 10, 10, 10, 11, 12, 13). They're ordered ascendingly, so every number will be either higher than or equal to the number above it. Now, I wish to remove all the duplicates from this list while remaining between 1 and 100. So, for example, for the values above, something like 2, 6, 7, 10, 11, 12, 13, 14, 15, 16 would work or 2, 6, 7, 8, 9, 10, 11, 12, 13, 14. However, the formulas I've tried so far will either go over 100, go under 1, or create circular references.
Given the nature of the list, there's very little chance of the amount of values exceeding 100, so if that possibility can be accounted for, it'd be a nice bonus, but it's not required.
Please create a named range for all your numbers and name it Source. As an alternative, replace the named range Source in my formulas below with the sheet address of the range where you have your numbers.
[C2] =INDEX(Source,MATCH(0,COUNTIF($C$1:$C1,Source),0))
This is an array formula and needs to be confirmed with Ctl + Shift + Enter. Observe that the COUNTIF range is defined one row above the row in which the formula resides. Its formulated with one absolute and one relative end. The end of the range will expand as you copy the formula down.
You can achieve a similar result using the LOOKUP() function. But since MATCH() finds the first instance, LOOKUP() returns the last. Therefore the formula below will return the same sequence in descending order. LOOKUP() is capable of looping on its own and the formula doesn't require array enabling. Just confirm it normally, with only Enter.
[E2] =LOOKUP(2,1/(COUNTIF($E$1:E1,Source)=0),Source)
Observe that the result range is defined with an absolute and relative end, starting above the row of the formula as explained above.
In B2, formula copied down :
=IF(A2="","",IF((COUNTIF(B$1:B1,A2)>0)+(A2=A1),B1+1,A2))
Edit #1
If data have repeated 100 as per OP's comment, then B2 formula become :
=IF((COUNTIF(B$1:B1,100)>0)+(A2=""),"",IF((COUNTIF(B$1:B1,A2)>0)+(A2=A1),B1+1,A2))
Thank you everyone for posting! Thanks to Bosco's original reply, I managed to fiddle around and find the answer myself. The biggest issue I was facing was getting a circular formula error, but by using Bosco's formula as a help column I was able to get it all worked out.
So, the solution holds 3 different columns: Original Data (column A), Help Column (column B) and Final Result (column C).
In the Help Column, I did as suggested, in cell B2 and copied down:
=IF(A2="","",IF((COUNTIF(B$1:B1,A2)>0)+(A2=A1),B1+1,A2))
This would occassionally create results over 100, so in the Final Column I placed the following array formula, which incidentally also accounts for lists longer than 100 entries, in C2 and copied down:
=IF(B2="","",IF(ROW(C2)>101,ROW(C2),IF(AND(B2>=100,B3=""),100,IF($B2:$B$1000>=C3,C3-1,B2))))
As an array formula, this needs to be entered using Ctrl+Shift+Enter.
Thank you very much to everyone who posted replies, I'm sorry for my lack of clarity!

In Excel, I would like to find the last value in a column that is less than each cell in the column

For example, My values are 5, 1, 6, 7, 8, 4, 7. I would like to find the last value that is less than each cell. The last value less than 5 would be 4. The last value less than 8 would be 7, etc. I'm currently using this formula:
=LOOKUP(9.999999999999E+307,IF(B2:$B$4204
But I'm having no luck. Thanks for your help.
This will return the last value in the list that is less than the value:
=INDEX(A:A,AGGREGATE(14,6,ROW($A$1:$A$7)/($A$1:$A$7<A1),1))
If you only want to look at the values under the current value change to:
=INDEX(A:A,AGGREGATE(14,6,ROW($A1:$A$7)/($A1:$A$7<A1),1))

Counting consecutive instances of numbers greater than value

I have a challenge to count the number of instances that consecutive values are greater than 12. I want to count the number of times that are consecutive in total. It might be easier to explain with an example of the data.
Data: 12.1, 12.3,13, 11, 10, 15, 13, 12.1, 11, 12.2, 13.1
Should return 5 - ie 5 times that 2 numbers next to one another are >12. NOT 3, where there 3 'groups' of cells all greater than 12 in each cell.
Current formula I am working with is:
=SUM(INT(FREQUENCY(IF(data>12,COLUMN(data)),IF(data<12,COLUMN(data)))>=2))
Where "data" is the range I am trying to count.
Assuming your data is in A1 to K1, this should work
{=SUM((A1:K1>12)*(B1:L1>12))}
Note that the {...} indicate an array formula, so copy the formula without them and enter with CTRL + SHIFT + ENTER.
The * is equivalent to AND in an array, so basically this formula sums up all instances, where both conditions within parenthesis are TRUE (TRUE = 1).

Resources