简体   繁体   English

Excel:查找列中的重复项,另一列中存在差异

[英]Excel: Find duplicates in column with differences in another column

I want to highlight cells in column A, which have duplicates in column A but a difference in column B. 我想突出显示A列中的单元格,这些单元格在A列中重复但在B列中有所不同。

A   B  
1   2   -
2   3   +
3   2   -
2   4   +
1   2   -
3   2   -
4   5   - 

The rows (or a cell within the row) with the - shall not be highlighted, but the rows (or a cell within the row) with the + shall be highlighted. 带-的行(或行中的单元格)不应突出显示,但带+的行(或行中的单元格)应突出显示。

How can I accomplish this in an Excel formula? 如何在Excel公式中完成此操作?

Please pay attention to the fact, that not all unique combinations shall be highlighted (last row!). 请注意以下事实:并非所有唯一组合都应突出显示(最后一行!)。

In SQL the corresponding query would be something like this: 在SQL中,相应的查询将如下所示:

SELECT *
FROM table
GROUP BY A
HAVING COUNT(B) > 1

A simpler solution might be to use Concatenate to join A and B together and use a conditional formating to highlight the unique values. 一个更简单的解决方案是使用Concatenate将A和B连接在一起,并使用条件格式突出显示唯一值。 This would leave your desired list highlighted: 这将使您所需的列表突出显示:

在此处输入图片说明

For the Conditional Formatting highlight column C then navigate: 对于条件格式突出显示列C,然后导航:

Home-> Conditional Formatting -> New Rule-> Format only unique or duplicate values Home->条件格式->新规则->仅格式化唯一值或重复值

Then change selection from "duplicate" to "unique" and select the desired format. 然后将选择从“重复”更改为“唯一”,然后选择所需的格式。 Apply the setting and have identified the appropriate rows. 应用设置并确定适当的行。

Assuming your data is in A1:B7, (with "A" and "B" as headers on row 1): 假设您的数据在A1:B7中(第1行的标题为“ A”和“ B”):

I used the following formulas to get the matches .. I just did a simple search after, and before .. if it finds a record above or below, it "flags" it in column F as TRUE. 我使用以下公式来获取匹配项。..我仅在..之前和之后进行了简单的搜索,如果它发现上方或下方的记录,则将其在F列中“标记”为TRUE。 Not sure it works for 3 or more duplicates, though you didn't seem to indicate how you wanted a 3 of a kind to work ;) 不确定它是否可以用于3个或更多重复项,尽管您似乎并没有表明您希望3种重复项起作用的方式;)

D2=MATCH(A2,A3:$A$1000,0)
E2=IF(ISERROR(D2),IF(ISERROR(G2),"",OFFSET($A$1,G2,0,1,1)),OFFSET(B2,D2,0,1,1))
F2=AND(NOT(AND(ISERROR(D2),ISERROR(G2))),B2<>E2)
G2=MATCH(A2,$A$1:A1,0)`

D col locates the first matching A column after the current row. D col在当前行之后找到第一个匹配的A列。

G col locates the first matching A Column prior to current row. G col在当前行之前找到第一个匹配的A列。

E col pulls that remote B column value to current row to more easily check. E col将该远程B列值拉到当前行以更轻松地进行检查。

F col puts the logic together: If we found something, and B cols are not equal. F col将逻辑放在一起:如果我们发现了一些东西,并且B col不相等。

Here is another way to do it assuming your above data is in cells A2:B7 : 假设您的上述数据在单元格A2:B7这是另一种方法:

1) Copy and paste your column A values to a blank section of your workbook(Lets say A11 ) and perform the following function Data->Remove Duplicates with the section selected. 1)将A列的值复制并粘贴到工作簿的空白部分(假设为A11 ),并在选定的部分执行以下功能Data->Remove Duplicates

2) Highlight cells B10:B13(all cells where a value is in column A) and type in the following formula: 2)突出显示单元格B10:B13(值在A列中的所有单元格),然后键入以下公式:

=FREQUENCY(A2:A8,A10:A13)

Hit Ctrl + Shift + Enter to make this an array. Ctrl + Shift + Enter使其成为数组。

3) Similar to step two highlight all cells in column C where there is data in columns A and B. In this case C2:C7 and use the following formula: 3)与第二步类似,突出显示C列中所有在A和B列中有数据的单元格。在这种情况下, C2:C7并使用以下公式:

=IF(VLOOKUP(A2,$A$10:$B$13,2,FALSE)>1,IF(FREQUENCY(VALUE(CONCATENATE($A$2:$A$7,$B$2:$B$7)),VALUE(CONCATENATE($A$2:$A$7,$B$2:$B$7)))<>1,"","Highlight"),"")

Hit Ctrl + Shift + Enter to make this an array. Ctrl + Shift + Enter使其成为数组。

Your cells that need to be highlighted will now say "Highlight" 现在需要突出显示的单元格将显示“突出显示”

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM