counting duplicates and assigning label - excel

In Excel suppose I have a table with the following two columns and following data:
ID Value
1 6
1 2
1 1
2 4
3 5
In excel what I would like to do is write the word duplicate in a third column (say result) when the id is duplicate and is not the highest value.
In this example duplicate would be written next to Value(2),ID(1) and Value(1),ID(1). Value(6), ID(1) would not have duplicate written next to it becasue it has the highest value out of all the ID(1)'s.
Is there an excel formula I can use to do this? If not what VBA would I need? In reality this is a large database and there will be more than 3 duplicates.
The result should look like this:
ID Value
1 6
1 2 Duplicate
1 1 Duplicate
2 4
3 5

Not sure if this is correct. But please correct me if I am wrong.
=IF(MIN($A$2:$A$6 = MIN($B$2:$B$6)), "duplicate", "")

This array formula should work (Ctrl+Shift+Enter) to confirm, though if you have lots of data could be rather slow.
=IF(B2=MAX(IF($A$2:$A$6=A2,$B$2:$B$6)),"","Duplicate")

if the duplicates are in column A, the cell B3 could read: (if ID are decreasing)
=if(COUNTIF($A$1:$A2,A3)>0,"Duplicate #" & COUNTIF($A$1:$A3,A3),"")
does this help?

Related

Looking to find Min and Max rows for various unsorted ranges in a column

I have a unique problem that I'm not sure can be solved easily in a spreadsheet.
I have a list of unsorted values that create various data ranges.
Values
Second Value
Desired Order
Value 1
Data A
1
Value 1
Data A
2
Value 1
Data A
3
Value 2
Data B
1
Value 2
Data B
2
Value 3
Data C
1
Value 1
Data A
1
Value 1
Data A
2
Value 1
Data A
3
Value 1
Data A
4
Value 2
Data B
1
Value 2
Data B
2
Value 2
Data B
3
See the example spreadsheet below. Thanks in advance for your help.
Example
SUMPRODUCT and COUNTIF seem to be close but not quite right.
The client has a file that has a static order. Since the column can not be sorted, it creates blocks of data ranges that contain a certain count of items.
Each time a new block starts, it should restart the number. Simply using a MIN and MAX do not work since there are multiple ranges of the same data spread throughout the column.
A block that repeats later in the file will simply continue in the numbered order using the last range's max value which is incorrect. Please look at the example data and the desired outcome and see if there are any remaining questions.
Notes:
I can not change the order so sorting is not an option.
As simple IF statement checking for changes will suffice here:
=IF(OR(B2<>B1,A2<>A1),1,C1+1)
Put that in C2 and copy down. Now if either A or B change it starts the count back at 1 else it adds 1 to the number above.
With Office 365 one can use SCAN to do the same thing at once:
=SCAN(0,ROW(A2:A14),LAMBDA(z,y,IF(OR(INDEX(A:A,y-1)<>INDEX(A:A,y),INDEX(B:B,y-1)<>INDEX(B:B,y)),1,z+1)))

excel, matching with greater than and lesser than and static values

I am trying to do in Excel what should be done in a database. I have a spreadsheet with raw data and which I am trying to query based on criteria. Given the following example table:
A B C D E F
1 Red up 1 4 dn 5
2 Blu up 5 9
3 Yel dn 1 4
4 Gre dn 5 9
I would like to return the value of column A that meets the criteria of E1 and F1 where E1 is found in column B and F1 is found equal or between the values in columns C and D. In the example, I would like to return "Gre".
I have been pulling my hair out with INDEX and MATCH functions and I can get part of my task done, but have found nothing extensible to solve the total solution.
Thank you in advance for your help!
Please try this...
=IFERROR(INDEX($A$1:$A$4,MATCH(1,INDEX(($B$1:$B$4=$E$1)*($C$1:$C$4<=$F$1)*($D$1:$D$4>=$F$1),),0)),"")
If you don't mind adding headings to your raw data.
You could use this formula:
=DGET($A$1:$D$5,"Field 1",$E$1:$F$2)
A1:D5 being your database.
Field 1 is the field to return values from.
E1:F2 is your criteria (field name and value to look for in that field).
https://support.office.com/en-gb/article/DGET-function-455568bf-4eef-45f7-90f0-ec250d00892e
As noted by #Vityata this won't work for the OP - looking for the value 6 would return a #VALUE error rather than Gre.
A couple of updates would allow it to work:
Updating the formula to: =DGET($A$1:$D$5,"Field 1",$E$1:$G$2)
Updating the table to:
The values in F2 and G2 are calculated as:
="<=" & $H$2 and =">=" & $H$2
This example would then return Yel when 1 is entered in cell H2.
I liked the question, thus I have elaborated a bit on the sktneer's answer.
The reason, it works is because we are looking for truth (a.k.a. 1) in the following formula:
=MATCH(1;
INDEX(($B$1:$B$4=$E$1)*($C$1:$C$4<=$F$1)*($D$1:$D$4>=$F$1););0)
Like this:
Then with an INDIRECT we may achieve the answer:
=INDIRECT(ADDRESSE(I1;1))
If you want it in one formula, it should be like this:
=INDIRECT(ADDRESS(
MATCH(1;INDEX(($B$1:$B$4=$E$1)*($C$1:$C$4<=$F$1)*($D$1:$D$4>=$F$1););0);
1))

Excel - count uniques in a range based on another field

I have a situation similar to Excel - Counting unique records in a group but with a final twist that's giving me a headache.
This is basically how my data looks:
A B C
--- --- ---
1 5 2
1 6 2
1 5 2
2 7 1
2 7 1
2 7 1
3 8 1
3 8 1
I'm trying to generate the value in column C. I need to count the number of unique values in column B for each different value in column A. Column A is sorted so that all of the values are together.
I've tried this:
=COUNTIFS($A$2:$A$8,$A2, $B$2:$B$1638,"<>"&"")
That gives me a count of the number of values in the group, but not the number of unique values in the group (so, in my example, it would 3, 3, and 2). I've also tried a pretty cool trick I found on this page which counts all of the unique values in the entire column (so in my example, it would be 4 all the way down). I can't figure out how to split the difference.
I've also tried to figure out if it can be done using the IF function, bu I'm coming up dry on that too. Any help here?
Use this array formula in cell D2:
=SUM(--(FREQUENCY( IF( $A$2:$A$9=A2, MATCH($B$2:$B$9,$B$2:$B$9,0)), ROW( $B$2:$B$9)-ROW( $B$2)+1)>0))
Put this in the formula bar and press CTRL + SHIFT + ENTER (instead of just ENTER) to save it as an array formula. Excel with place these brackets { } around your formula.
Then copy it down. It works well, just be aware that array formulas can get very slow if you use thousands of them in a workbook.
I found this following a link to here from the "cool trick" you linked to.
If it works let me know, and don't forget to mark my answer as accepted. Good luck!

Merge two values in if both values are same except blank in EXCEL

I want to merge two cells in excel that have a value like this:
'a b c
1 1 11
1 2 12
1
2
1
2 2 22
'
I don't want to merge the number with a blank...
any help for that? I used concatenate function but it cannot help me ...
You can produce column C using an if statement and the "&" operator.
=IF(LEN(B2)<1,"",A2&B2)
If you want C to be blank in the case where A is blank, then you'll need an or statement also.
Assuming the data are orientated in columns A and B starting at row 1, =IF(OR(ISBLANK(A1), ISBLANK(B1)),"",CONCATENATE(A1,B1)) is one way. Copy this downwards.
The returned value is a string type. A blank string is inserted into rows where either a or b are not given.
=IF(VALUE(A1)=VALUE(B1),CONCATENATE(A1,B1),"")
OR
=IF(A1=B1,CONCATENATE(A1,B1),"")

Same value then increment

Im new to VBA and need help making a macro to basically see if the value is the same in the column and increment in another column. Bit confusing so heres an example:
INBOUND.T28 1
INBOUND.T28 2
INBOUND.T28 3
INBOUND.T28 4
INBOUND.T29 1
INBOUND.T29 2
INBOUND.T29 3
INBOUND.T29 4
INBOUND.T29 5
INBOUND.T30 1
INBOUND.T30 2
INBOUND.T31 1
INBOUND.T31 2
INBOUND.T31 3
Column A is already in the file, I want column B to print as above. Its basically counting the number of same values in A and restarting count when it a new value is found.
Im not really any good with VBA - although I have done Java in the past and based of that I know that I would need to use some kind of IF statement and a counter such as
value = value + 1
and maybe something like this to count?
Range("A1").Value = Range("A1").Value + 1
Please help, I have no idea how to do VBA and learning it as I go :P Thanks! –
Assuming 'INBOUND.T28' is in Column A and the 1 next to it in Column B then this formula should suit in B2:
=IF(A1=A2,B1+1,1)
for the first row of data, if your first row is blank or labels. You can fill this and subsequent Column B rows with VBA with Application.WorksheetFunction.

Resources