Excel find duplicate cells in multiple columns - excel

I have an Excel table with 4 columns and need to find the values that are the same in ALL 4 of the columns and put them aside. Trying to figure out how to accomplish this in Excel.
Example:
Column 1 Column 2 Column 3 Column 4
A1 B1 B1 A1
B1 C2 C1 B1
C2 D3 C2 C2
In this example, both B1 and C2 values comply and must be set aside.

Put this formula in cell D1 and drag and drop it till the end of your values:
=IF(AND(COUNTIF($B$1:$B$3,A1)>0,COUNTIF($C$1:$C$3,A1)>0,COUNTIF($D$1:$D$3,A1)>0),A1,"not found")
For the record, the french version:
=SI(ET(NB.SI($B$1:$B$3;A1)>0;NB.SI($C$1:$C$3;A1)>0;NB.SI($D$1:$D$3;A1)>0);A1;"not found")

To generate a list of values that appear in all four columns, try in cell F2:
=INDEX(A$1:A$3,MATCH(TRUE,COUNTIF(B$1:B$3,A$1:A$3)*COUNTIF(C$1:C$3,A$1:A$3)
*COUNTIF(D$1:D$3,A$1:A$3)*NOT(COUNTIF($F$1:F1,A$1:A$3))>0,0))
confirmed with CTRL+SHIFT+ENTER and fill down as far as needed (this assumes F1 is blank or contains unrelated data)

I couldn't get the above examples to work for myself, so I came up with another solution. Hopefully this will help someone else out there:
=IF(ISNUMBER(MATCH($A2, $B$2:$B$4, 0)), IF(ISNUMBER(MATCH($A2, $C$2:$C$4, 0)), IF(ISNUMBER(MATCH($A2, $D$2:$D$4, 0)), $A2, ""), ""), "")
This checks the value in "Column 1" ("A" column in excel) against the next 3 columns (B,C,D). If the value is present in all three it returns the A column value. If it is not present in all 3, it returns empty. If you would rather return something else, put that something in between the empty quotation marks.
For your data, enter in the lengths of the searched columns, B through D. For example, where it says, "$B$4", put in "$B$99" if the cells go down to 99, but it should work if you put in a generically high number, for example, 99999.
Put this in the row of the first value, so here that would be E2, and then copy it down to every row that has a value in column A.
The function works by just nesting IF statements, and can be expanded or contracted depending on how many columns you want to check against. The function can also be easily modified into a "check if in column B OR column C" function pretty easily.

here is an example for 1 value; I can't generalize since I did not see your data source
Sub test()
If (Range("D6").Value = Range("D6").Offset(0, 1).Value) Then
MsgBox "match"
End If
End Sub

you can also delete duplicates across multiple Columns by using DATA-> Remove Duplicates.

Related

adding specific values to excel column when certain conditions are met

I have 3 columns,
Afghanistan
Benin
Country
The first 2 columns contains decimal values in them and also some blank spaces in between(represented by '-'). What I want to achieve is that I want to check if Afghanistan column has any value in it, if yes, then the corresponding cell of country column in that row should have Afghanistan in it and if in that same row, Benin column has value, then the Country column should have Benin in it and if both have values in them then Country cell should have Afghanistan, Benin in it. Also, if there is a value already present in the Country column, then it should not be changed. How can it be achieved?
Please help me as I'm a total newbie in excel.
Thanks
Please see this picture
What you might need is the =ISBLANK() worksheet function, which can check if a cell is empty or not. This is to be combined with an =IF() formula:
=IF(ISBLANK(A2);"Value if true";"Value if false")
When you put this, let's say in cell B2, you'll get the expected result.
When you want to check if anything is present inside a column, you might do this:
=LEN(TEXTJOIN("",FALSE,B2:B6))
This concatenates column B (from B2 to B6), using an empty string delimiter, and takes the length of the result. In case there's nothing in that entire column, the length will be zero.
Without VBA, assuming a new Country (I name it Country2) column as a solution is acceptable.
Also assume the the 3 original column is A:C & Country2 column is D. Put this is D2 and drag downwards :
=IF(C2="",IF(AND(B2="-",A2="-"),"",IF(B2="-","Afghanistan",IF(A2="-","Benin","Afghanistan,Benin"))),C2)
Idea : convert all you logic to nested if. The Column D is your desired column.
how it work:
=IF(C2="", ... ,C2) If C2 is not empty, take C2 value. If It is empty, do (...)
IF(AND(B2="-",A2="-"),"", ... ) If both A2 & B2 is empty put a blank : "" , else do (...)
IF(B2="-","Afghanistan", ... ) If only B2 is equal to "-", put "Afghanistan", else do ...
IF(A2="-","Benin","Afghanistan,Benin") If only A2 is equal to "-", put "Benin", else put "Afghanistan,Benin" .

excel SUMIFS only on same date

I'm trying to create a formula in column K which sums all cells that apply , in column J, only when the following conditions are true:
dates are the same in column A
AND client name is the same in column B
For example, in cell K2, I want the sum of J2+J3+J4 because A2=A3=A4 and B2=B3=B4.
K5=J5 only, because there are no other dates with the same client name.
K6=J6+J7 because A6=A7 and B6=B7.
What kind of formula would I use for this? I can't figure out how to do it with a SUMIFS.
I would try using a pivot table with:
The names as row values
The dates as the column values
And funds received using SUM in the values column
Edit
Based on #pnuts comments here is how to get the values in column K. Put this in K2 and drag down.
=IF(OR(COUNTIFS($B$1:B3, B3) = 1, B3 = ""), SUMIFS($J$2:J2, $A$2:A2, A2, $B$2:B2, B2), "")
This formula will give blank values until the formula finds a new client on a new date. However, I still think using pivot table is a better solution.
However, I still find the pivot table
In cell K2 put following formula:
=IF(COUNTIFS($A$2:A2,A2,$B$2:B2,B2)=1,SUMIFS($J$2:$J$10,$A$2:$A$10,A2,$B$2:$B$10,B2),"")
Adjust row 10 value. It will be last row of your actual data.
Copy down as much you need.
EDIT
Uploaded file shows the cause behind formula not working correctly for you. It turned out to be whitespace characters in column B (names) data e.g.
Cell B3: "Moe John" has a trailing space.
Cell B10: Same case with "Doe Jane"
If you want to use above posted formula then all names shall be corrected. Or alternatively to deal with spaces you can adopt below approach.
=IF(COUNTIFS($A$2:A2,A2,$B$2:B2,"*"&TRIM(B2)&"*")=1,SUMIFS($J$2:$J$28,$A$2:$A$28,A2,$B$2:$B$28,B2),"")
Notice the change in COUNTIFS formula where B2 is now replaced with "*"&TRIM(B2)&"*".
Even such formula will take a beating if you have uneven whitespace characters in between your data. I'd suggest normalizing it as much as possible.

Search a column of text by adjecent cell and return a binary value (excel)

What's a good way to search a column of text (e.g. A1:A10) by adjecent cell (e.g. B1) and return a binary value (e.g. 0 or 1) in the next column (e.g. C1:C10)?
my 'un-elegant' approach so far: use VLOOKUP, then filter and delete all fields that return an error.
A simple MATCH will do for you
=IF(ISNUMBER(MATCH("*"&B1&"*",A1:A10,0)),1,0)
if I understand your question correctly you are wanting column C to show 1 or 0 depending on whether text in column B appears in text in column A, if so:
formula in C2 to extend down =if(find(B2, A2) > 0, 1, 0)
FIND will return the location of the first occurrence of the first argument within the second
EDIT
Ok, if you want to check all of column A use this:
=OR(NOT(ISERROR(FIND(B1, A1:A8))))
Again its array formula so complete with CTRL+SHIFT+ENTER
So its doing a FIND for B1 against all of column A, any row that doesn't contain B1 is going to return an error. So now you have an array whose values are either ERROR or a number indicating B1 is found, you can then use the ISERROR function on each element of the array and then NOT each element, you will then have a TRUE for each row B1 appears in, then collapse it all to a single value using OR :) you can extend this formula in B1 down for the other rows but make sure to lock A1:A8 as in $A$1:$A$8

Excel: search if a specific text exists in a column

I have two columns. Each cell in column A contains a full sentences and each cell in column B contains a word or phrase. I would like to check if the contents of each cell in column B appears in one of the cells in column A---it could appear in multiple cells in column A or in no cells. The output just needs to be a yes or no (and should be spit out in column C) for my purposes, but it would be neat to return the number of times each column B word came up somewhere in Column A.
So far I haven't figured out how to take a discrete string of letters (already printed in one cell) and search across a range in a column. Not sure if this is beyond the regular excel functionality.
Thanks very much for your help!
Use array formula like this:
=SUM(IF(ISERROR(SEARCH(B1,A:A,1)),0,1))
enter in formula bar then press CTRL+SHIFT+ENTER.
Hope this helps.
Put formula in C.
Try This :
=countif(a:a,"*" & b2 & "*")>0 gives you result in True/Flase
To get the occurrence
=countif(a:a,"*" & b2 & "*")
To get YES/NO
=if(countif(a:a,"*" & b2 & "*")>0,"YES","NO")

Retrieving Values From Excel Merged Columns

I'm doing a hlookup against a value that spans multiple columns. My data is similar to this:
A B C D
---------------------------
1| Col1 Col2
2| x y z w
3|
4|
In rows 3 and 4 (A3, B3, C3, D3, etc.), I'd like to put formulas that will do an hlookup somewhere else in the workbook. The trick is, I'd like it to look up "Col1" for columns A and B and "Col2" for columns C and D. "Col1" is in A1, but is really A1 and B1 merged. When I reference A1, "Col1" appears, but when I reference B1, the return value is blank.
Any ideas?
Here is another solution that can also work when the merged cells are of different widths, let me illustrate with an example:
Open a fresh Excel, merge B1, C1, D1
Type Col1 in the merged cell
In B2, type formula =B1, and in C2 =C1, in D2 =D1
You should see B2 to be Col1 while C2, D2 are 0
In B3, type the formula =A3, copy it
Right-click the merged cell B1:D1, select "paste special -> formulas"
You should see the merged cell being 0
Type Col1 in the merged cell
You should now see all B2, C2, D2 to be Col1, i.e. now you can reference the merged cell as you expect it to be.
If you can multiple merged cells, each of different widths, just paste the formula to all of them in one go.
The reason behind this works is because of a perculier design choice by Microsoft. It seems that when you paste formulas in merged cells, each underlying cell receives the formula (in contrast, if you enter a value, only the top-left cell gets it) So you can use it at your advantage and paste a formula that reference the cell next to it, and then overwrite the top-left cell with the value you want, then every cell underlying the merged cell will have that value.
To get access to the "Col1" and "Col2" labels, you can use the following:
=INDEX($1:$1,1,COLUMN()-MOD(COLUMN()-1,2))
Note: This assumes that you are grouping together the same number of cells. If it were three cells, you would just change the last number in the formula to a 3, and so on.
Edit: Here's how it works:
INDEX($1:$1,1, x ) returns the value of the cell in row 1, column x. If your table is not actually located in the top left corner of the worksheet, you can change this to the actual range that includes all of your merged labels. In this case, it would be:
INDEX($A$1:$D$1,1, x )
COLUMN() returns the column number of the current cell (1 in column A, 2 in column B, etc.)
MOD(COLUMN()-1,x) returns an offset from the current column to the column that holds the proper label
I've built a simple function in vba that will solve this problem:
Function mergedText(rngMergedCell As Range)
If rngMergedCell.MergeCells = True Then
mergedText = rngMergedCell.MergeArea(1, 1)
Else
mergedText = rngMergedCell
End If
End Function
If the cell is a merged cell, the function will return the value in the first element of the merged cell - this is where the merged cell stores its value
A more generic variant of e.James's proposal is :
={INDEX($A$1:A1, 1, MAX(NOT(ISBLANK($A$1:A1))*COLUMN($A$1:A1)-COLUMN($A$1)+1))}
This relies on the fact that the merged cells are empty except for the first one (unless you are in a case like Martin's proposal).
Note: The curly braces are there to mark an array formula (do not enter them, just press alt+return to validate the formula in the cell).
I realize I am late to this thread but I found a really simple answer to this.
If, for example, your label is merged across 4 columns a1:d1, and if you reference b1, you will return "". For dynamically finding the right labels use this fx in your new table:
=if(OriginalTable!B1="",ThisTable!A1,OriginalTable!B1)
I am sure you will realize that this will capture ranges in e1:h1 etc as you drag across.
That's it. Hope it helps someone.
Cells B1 and D2 contain no values, only A1 and C1 have something inside them.
So you'll just have to make sure that your formulas in columns A and B both refer to A1 as the lookup value, and that your formulas in columns C and D both refer to C1 for the lookup value.
With the new dynamic reference, there are more options now. Here is a generic function I wrote that will search to the left of the cell and return the first value. It is NOT optimized, but it does the job for me.
=LET(
TargetCell, A1,
TargetRow, ROW(TargetCell),
TargetCol, COLUMN(TargetCell),
RowReference, INDIRECT(TargetRow & ":" & TargetRow),
RowValues, TRANSPOSE(FILTER(RowReference,ISBLANK(RowReference)=FALSE)),
RowValueColumns, MATCH(RowValues, RowReference,0),
ReturnColumn, MAX(FILTER(RowValueColumns,RowValueColumns<=TargetCol)),
Return, INDIRECT(ADDRESS(TargetRow,ReturnColumn)),
Return
)

Resources