Excel - finding duplicate values over multiple columns - excel

I have a data set containing phone numbers three different users. Each one is its own column in Excel. Eg Alice called the numbers in A:A, Bob called the numbers in B:B and Carol called the numbers in C:C (each column is a different length).
I am trying to determine which phone numbers all 3 have called. I have used unique to filter out duplicates from each list and I know how to find duplicate values over two columns using conditional formatting. But how do I find values that ONLY appear in all three columns?
Clarification:
A mockup of the data is as follows (I can't give out the real data):
| 1 | 5656 | 6464 | | | |-----|------|------|---|---| | 2 | 1 | 456 | | | | 456 | 2 | 2 | | | | 345 | 800 | 1 | | |
I want excel to look through the three columns and find any values which are common to all 3 columns and highlight them.

Let's assume the input data is in the range of A2:C7, you can try the following formula in E2:
=LET(rng, A2:C7, ux, UNIQUE(TOCOL(rng,1)),DROP(REDUCE("", ux, LAMBDA(ac, x,
LET(cmp, N(rng=x), ones, SEQUENCE(,ROWS(cmp),1,0), IF(SUM(MMULT(ones, cmp))=3,
VSTACK(ac,x), ac)))),1))
A simplified approach can be achieved using COUNTIFS:
=LET(rng, A2:C7, ux, UNIQUE(TOCOL(rng,1)), FILTER(ux, COUNTIFS(rng,ux)=3))
Here is a sample input data and the corresponding output. Highlighted the numbers the formula should return (see conditional formatting formula below):
The previous solutions assumes there are no repeated numbers per column in the input range (rng). Under this assumption you can define a Conditional formatting as follow:
=SUM(N(A2=$A$2:$C$7))=3
If the number can be repeated within a given column, then the first formula can be adjusted as follow:
=LET(rng, A2:C7, ux, UNIQUE(TOCOL(rng,1)),DROP(REDUCE("", ux, LAMBDA(ac,x,
LET(cmp, N(rng=x), ones, SEQUENCE(,ROWS(cmp),1,0),
IF(SUM(N(MMULT(ones, cmp) >= 1))=3, VSTACK(ac,x), ac)))),1))
Both solutions should work regardless of the number of rows of the input columns. The second input argument of TOCOL(1) ensures empty cells are not taken into account.

You will need to change the ranges, I used just 3 rows to test it.
Sub FindCommonNumbers()
Dim dict As Object
Dim rngAlice As Range, rngBob As Range, rngCarol As Range
Dim cell As Range
Dim phone As Variant
' Create a new dictionary object
Set dict = CreateObject("Scripting.Dictionary")
' Set the range for each user's phone numbers
Set rngAlice = Range("A1:A3")
Set rngBob = Range("B1:B3")
Set rngCarol = Range("C1:C3")
' Add the phone numbers from the first column to the dictionary
For Each cell In rngAlice
dict(cell.Value) = 1
Next cell
' Check the phone numbers in the second column against the dictionary
For Each cell In rngBob
If dict.exists(cell.Value) Then
dict(cell.Value) = dict(cell.Value) + 1
End If
Next cell
' Check the phone numbers in the third column against the dictionary
For Each cell In rngCarol
If dict.exists(cell.Value) Then
dict(cell.Value) = dict(cell.Value) + 1
End If
Next cell
' Print the phone numbers that appear in all three columns
For Each phone In dict.Keys()
If dict(phone) = 3 Then
Debug.Print phone
End If
Next phone
End Sub

Related

Summing drop down text values

I've got a sheet with 4 columns that are drop-down lists which describe an arbitrary product. I need a formula that will assign a numerical value to one of the drop down selections, then sum up all of the numerical values for that row. Across the 4 columns there are 9 criteria that can be selected. I've tried:
=sum((if(b2="x", 1), if(b2="y", 2), if(c2="a", 0.5), if(c2="d", 0.3) and so forth, but the formula is just too long
You need to create a table of data to lookup. You can create it in a new sheet.
x | 1
y | 2
a | 0.5
d | 0.3
The vlookup takes an input e.g. x and looks at the range of the table of data and returns 1
=vlookup(B2,LookupSheet!A1:B5,2,false)
=vlookup([what to look for],[range to search],[column in range to return],[type of search, false means exact match]
What Gary's Student is suggesting is
=sum(vlookup(B2,LookupSheet!$A$1:$B$5,2,false),vlookup(C2,LookupSheet!$A$1:$B$5,2,false))

Excel rotate (NOT transpose) a table 90 degrees clockwise

I have a table in Excel. How can I rotate (NOT transpose) a table 90 degrees clockwise?
Example
cell1 | cell2 | cell3
cell4 | cell5 | cell6
Expected
cell3 | cell6
cell2 | cell5
cell1 | cell4
P/S: If I use transpose option when paste, I will have unexpected result
cell1 | cell4
cell2 | cell5
cell3 | cell6
Thanks
Assuming you want to do this manually, and that it doesn't need to be done via functions:
First transpose your range using the regular copy transpose paste function
Next, create an artificial sorting key (simply number is 1 to n) infront of the first column and sort in a descending order
Delete the sorting column you created in front of the first row
You have to go in a two steps process:
transpose your table (you have already done it)
"mirror" your table
To do this last step, consider that you have a table M rows x N columns, which is transposed into a N rows x M columns. The cell located at row i and column j has to be moved to the row i-1 + N and column j.
In your example, after transposition cell 1 is in (1,1) with N = 3, and it will go to (1-1+3,1)=(3,1) and so on.
You need to use a short macro to do the transformation.
If you can use the new feature of SORTBY in excel,
assuming your table sits in A1:C2, you just need to =SORTBY(TRANSPOSE(A1:C2), TRANSPOSE(COLUMN(A1:C2)),-1) to allow the data sorted by the natural reverse order provided by COLUMN after you transpose it.

Excel function for comparing columns with repeated values

I'm using excel and have two columns (A & B) with values
I want to search for each value in Column A and return the position in Column B.
I'm using this formula:
IFERROR(MATCH("Values in Column A";"Array = Column B";0);0)
The results are:
Column A | Column B | Column C
1 | 4 | 2
2 | 1 | 3
3 | 2 | 4
4 | 1 | 1
1 | 2 | 2
| 3 |
It works fine if it doesn't encounter repeated values. However, I want it to encounter repeated values, so the formula should ignore the ones it was encountered before and go through the others. So the correct result should look like this:
Column C
2
3
5
1
4
Can you help me on this? Is there a VBA routine for this?
From the article Getting the 2nd matching value from a list using VLOOKUP formula, you can create a helper column to affix the instance number of each value, to create unique id's.
For example, in Column C, add the following function:
=A1&"-"&COUNTIF($A$1:A1,A1)
Note: The relative reference on the count range will cause the applicable range to grow as it is dragged down. The count of the items matching that cell in a range containing only that cell should always be one. As it gets dragged down to include other cells, it will increment accordingly.
Then add the same thing in Column D to get the instances of cells in Column B:
=B1&"-"&COUNTIF($B$1:B1,B1)
Finally, do the math you want to do in Column E like this:
=IFERROR(MATCH(C1,D:D,0),0)

Excel - return value based on two inputs

I have two input values, that I want to use to return a third value.
Input 1: Y (lets say "Y" is in cell B1)
Input 2: 15 (15 in cell B2)
In another database sheet the input 1 values are sorted in the top row, the input 2 values are listed in a column in front of the wanted values.
| | X | Y | Z |
|16| a | g | k |
|15| b | h | l |
|14| c | i | l |
Fx. X,Y,Z are in row 3 and column 2,3,4.
I want a formula that returns "h" from the two inputs, Y & 15. How is this possible?
In your example above:
=INDIRECT(B1&B2) will return the value in [Y15] which is "h"
Assuming 16 is in Row4, please try:
=INDIRECT(CHAR(CODE(B1)-22)&6-MOD(B$2,14))
Here's a formula for you:
=INDEX(MatrixRange,MATCH(RowInput,MatrixStartColumn,0),MATCH(ColumnInput,MatrixStartRow,0))
MatrixRange = whatever the range of your data matrix is (in entirety) so if your data matrix starts in A3 and ends in D20 this would be replaced with A3:D20
RowInput = whatever cell you are getting search value for to find the row of appropriate data (This is Input 2 on your example)
MatrixStartColumn = Whatever column (or range) your Matrix data index starts in (these would be the numbers in your example). If the numbers on the left side of your data example are in Column A, this would be changed to A:A or A1:A50 (or wherever the last value is). The important thing for this is to use a range from the start of the column otherwise your row count will be off. If you must use a sub-range to avoid matches outside of the matrix, be sure to add the appropriate number to the end of the Match statement. For example if you are specifying MatrixStartColumn as "A3:A44", you will need to add +2 for the first 2 rows being skipped (A1 and A2). So the Index statement becomes (MatrixRange,MATCH(RowInput,MatrixStartColumn,0)+2,...
ColumnInput = whatever cell your column search value is in (your Input 1 data)
MatrixStartRow = Same as StartColumn above but for the header index of your matrix (the XYZ letters in your example). Just as above, if you must use only the range of the matrix, be sure to add your offset numbers so you get the right column.
Assuming that your inputs are at B1 (row), and B2 (Column) here is your formula:
=INDEX(B4:D6,MATCH(B1,A4:A6,0),MATCH(B2,B3:D3,0))
Here is how the formulas work:
INDEX(area with values, row , column) returns value based on row and column you provide.
MATCH(value to find, range to search) returns row/column where value was found.
Note that Match will accept only one row or column for 'range to search'.

Excel Function Help - Compare 2 Cells in Worksheet to 2 Cells in another worksheet, if they match, return value from a different column

I'm wondering if someone would be able to offer some advice on the best way to do this please:
Data in Worksheet # 1
Col A | Col B | Col C
Part-1 | 8 | 2
Part-2 | 7 | 7
Part-7 | 9 | 4
Part-8 | 2 | 6
Data in Worksheet # 2
Col A | Col B | Col C
Part-1 | 8 | *Return value* (If Part-1 is found and 8 matches, return 2)
Part-2 | 7 | *Return value*
Part-3 | 8 | *Return value*
In Worksheet#2 in Cell C2 - I would like to check if the Part-1 in A1 is found in Col A in Worksheet#1. If it is, then I would also like to make sure the Number is B2 in Worksheet#2 matches the Qty in Col B next to the same part#, if both the Part# and Qty match, then i would like to copy across the value from the corresponding cell in Col C in Worksheet#1, to Col C in Worksheet#2. If it does not match, I would like it to return a 0.
Here is the a variation on Reinier's second approach in a form that will work in any version of Excel. You can use references to the specific data ranges, as I have done here, or to entire columns.
=SUM((A2=Sheet1!$A$2:$A$5)*(Sheet2!B2=Sheet1!$B$2:$B$5)*Sheet1!$C$2:$C$5)
This is an array formula, so it needs to be entered with the Control-Shift-Enter combination. It performs the same operation as the SUMPRODUCT. (There are several other ways to do this, such as using MATCH with INDEX or OFFSET.)
Essentially, what you are doing is a lookup based on values in two columns. Looking at it from that angle, you can use a the SUMPRODUCT function, which is supported in any version of Excel. Enter the following in Sheet2, cell C2:
=SUMPRODUCT((Sheet1!$A:$A=$A2)*(Sheet1!$B:$B=$B2)*Sheet1!$C:$C)
Select and drag down the right-bottom corner of C2 to complete column C.
This only works by virtue of the fact that the values in Sheet1, column C, are numbers. It breaks if value pairs in column A and B of Sheet1 occur multiple times, but you did not address that situation in your question in the first place.
For versions 2007 and up, you can use the more convenient function SUMIFS with basically the same approach:
=SUMIFS(Sheet1!$C:$C,Sheet1!$A:$A,$A1,Sheet1!$B:$B,$B1)
Alternatively, you can use a combination of IF and VLOOKUP functions. A formula that will work for Excel 2007 or newer is the following:
=IFERROR(IF(VLOOKUP($A1,Sheet1!$A:$C,2,FALSE)=$B1,VLOOKUP($A1,Sheet1!$A:$C,3,FALSE),0),0)
Older versions of Excel do not support IFERROR, but you can still use a similar approach as explained here.
I have uploaded an example workbook here, which includes an alternative method in column D of Sheet2 as well.

Resources