跳到主要内容

如何从Excel中的多个列中提取唯一值?

假设您有多个具有多个值的列,则某些值在同一列或不同列中重复。 现在,您只想查找任一列中存在的值一次。 是否有任何快速技巧可以从Excel中的多个列中提取唯一值?


使用公式从多列中提取唯一值

本节将介绍两个公式:一个使用适用于所有 Excel 版本的数组公式,另一个使用专门针对 Excel 365 的动态数组公式。

使用适用于所有 Excel 版本的数组公式从多列中提取唯一值

对于使用任何版本的 Excel 的用户来说,数组公式可以成为跨多列提取唯一值的强大工具。您可以这样做:

1。 假设您的值在范围内 A2:C9,请在单元格E2中输入以下公式:

=INDIRECT(TEXT(MIN(IF(($A$2:$C$9<>"")*(COUNTIF($E$1:E1,$A$2:$C$9)=0),ROW($2:$9)*100+COLUMN($A:$C),7^8)),"R0C00"),)&""
备注:在以上公式中, A2:C9 表示要提取唯一值的单元格范围, E1:E1 是您要放置结果的列的第一个单元格, $ 2:$ 9 这些行包含要使用的单元格,并且 $ A:$ C 表示列包含您要使用的单元格。 请更改为您自己的。

2。 然后按 Shift + Ctrl + 输入 键,然后拖动填充手柄以提取唯一值,直到出现空白单元格。 看截图:

这个公式的解释:
  1. $ A $ 2:$ C $ 9:指定要检查的数据范围,即A2到C9的单元格。
  2. IF(($A$2:$C$9<>"")*(COUNTIF($E$1:E1,$A$2:$C$9)=0), ROW($2:$9)*100+COLUMN($A:$C), 7^8):
    • $A$2:$C$9<>"" 检查范围内的单元格是否不为空。
    • COUNTIF($E$1:E1,$A$2:$C$9)=0 确定这些单元格的值是否尚未列在从 E1 到 E1 的单元格范围中。
    • 如果满足两个条件(即值不为空且尚未在 E 列中列出),IF 函数将根据其行和列计算唯一数字 (ROW($2:$9)*100+COLUMN($A: $C))。
    • 如果不满足条件,该函数将返回一个大数字 (7^8),用作占位符。
  3. 最小值(...):查找上面 IF 函数返回的最小数字,对应下一个唯一值的位置。
  4. 文本(...,“R0C00”):将此最小数字转换为 R1C1 样式地址。格式代码 R0C00 表示将数字转换为 Excel 单元格引用格式。
  5. 间接(...):使用INDIRECT函数将上一步生成的R1C1样式地址转换回正常的A1样式单元格引用。 INDIRECT 函数允许基于文本字符串的内容进行单元格引用。
  6. &"":在公式末尾添加 &"" 可确保最终输出被视为文本,因此偶数将显示为文本。
 
使用 Excel 365 公式从多列中提取唯一值

Excel 365 支持动态数组,使得从多列中提取唯一值变得更加容易:

请将以下公式输入或复制到要放置结果的空白单元格中,然后单击 输入 一次获取所有唯一值的关键。看截图:

=UNIQUE(TOCOL(A2:C9,1))


使用 Kutools AI Aide 从多列中提取唯一值

释放的力量 Kutools 人工智能助手 从 Excel 中的多个列中无缝提取唯一值。只需点击几下,这个智能工具就会筛选您的数据,识别并列出任何选定范围内的唯一条目。忘记复杂公式或VBA代码的麻烦;拥抱效率 Kutools 人工智能助手 并将您的 Excel 工作流程转变为更高效且无错误的体验。

备注: 要使用这个 Kutools 人工智能助手 of Kutools for Excel下载并安装 Kutools for Excel 第一。

安装 Kutools for Excel 后,请单击 Kutools人工智能 > 人工智能助手 打开 Kutools 人工智能助手 窗格:

  1. 在聊天框中输入您的要求,然后单击 提交 按钮或按下 输入 发送问题的键;
    “从范围 A2:C9 中提取唯一值,忽略空白单元格,并将结果从 E2 开始:”
  2. 分析完后点击 执行 按钮运行。 Kutools AI Aide 将使用 AI 处理您的请求,并直接在 Excel 中返​​回指定单元格中的结果。


使用数据透视表从多个列中提取唯一值

如果您熟悉数据透视表,则可以按照以下步骤轻松地从多个列中提取唯一值:

1。 首先,请在数据左侧插入一个新的空白列,在本例中,我将在原始数据旁边插入A列。

2。 单击数据中的一个单元格,然后按 Alt + D 键,然后按 P 立即打开钥匙 数据透视表和数据透视图向导,选择 多种合并范围 在向导step1中,查看屏幕截图:

3。 然后点击 下一页 按钮,检查 为我创建一个页面字段 向导步骤2中的选项,请参见屏幕截图:

4。 继续点击 下一页 按钮,单击以选择包括左侧新的单元格列的数据范围,然后单击 地址 按钮将数据范围添加到 所有范围 列表框,请参见屏幕截图:

5。 选择数据范围后,继续单击 下一页,在向导的第3步中,根据需要选择要放置数据透视表报表的位置。

6. 最后点击 完成 以完成向导,并在当前工作表中创建了一个数据透视表,然后取消选中 选择要添加到报告中的字段 部分,请参见屏幕截图:

7. 然后检查现场 或将值拖到 标签,现在您将从多个列中获得唯一值,如下所示:


使用VBA代码从多个列中提取唯一值

使用以下VBA代码,您还可以从多个列中提取唯一值。

1。 按住 ALT + F11 键,然后打开 Microsoft Visual Basic for Applications窗口.

2。 点击 插页 > 模块,然后将以下代码粘贴到“模块窗口”中。

VBA:从多个列中提取唯一值

Sub Uniquedata()
'Updateby Extendoffice
Dim rng As Range
Dim InputRng As Range, OutRng As Range
Set dt = CreateObject("Scripting.Dictionary")
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
For Each rng In InputRng
    If rng.Value <> "" Then
        dt(rng.Value) = ""
    End If
Next
OutRng.Range("A1").Resize(dt.Count) = Application.WorksheetFunction.Transpose(dt.Keys)
End Sub

3。 然后按 F5 运行此代码,将弹出一个提示框,提醒您选择要使用的数据范围。 看截图:

4。 然后点击 OK,将出现另一个提示框,让您选择放置结果的位置,请参见屏幕截图:

5. 点击 OK 关闭此对话框,并立即提取所有唯一值。


更多相关文章:

  • 计算列表中唯一值和不同值的数量
  • 假设您的值列表很长,其中包含一些重复项,现在,您要计算唯一值(仅一次出现在列表中的值)或不同值(列表中所有不同值的数量)的数量值+第一个重复值),如左图所示。 本文,我将讨论如何在Excel中处理此工作。
  • 根据Excel中的条件提取唯一值
  • 假设您具有以下数据范围,您希望根据列A的特定标准仅列出列B的唯一名称,以得到结果,如下面的屏幕快照所示。 您如何在Excel中快速轻松地处理此任务?
  • 仅在Excel中允许唯一值
  • 如果您只想在工作表的列中输入唯一值并防止重复,则本文将介绍一些快速技巧来帮助您完成此任务。
  • 根据Excel中的条件求和唯一值
  • 例如,我现在有一系列数据,其中包含“名称”和“订单”列,以根据“名称”列仅对“订单”列中的唯一值求和,如下图所示。 如何在Excel中快速轻松地解决此任务?

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (31)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Thank you for this solution, however what if the columns are in separate excel sheet? or is in separate columns instead of a table?
This comment was minimized by the moderator on the site
Hello, Jon,
The methods in this article ar only works well for a range of data, if your data in separate columns, you should copy and paste them into one range first, and then apply the formula or VBA code.
Thank you!
This comment was minimized by the moderator on the site
Thank you for this great article.

For people who are using the array formular in non-English Excel there must be taken special care of the text format string: in your example: "R0C00".
For German this would translate to "Z0S00". However, "S" is a special character refering to seconds for time formating. This character needs to be escaped and therefore the correct format string for German Excel is "Z0\S00".

I hope this helps someone in the future :-)
This comment was minimized by the moderator on the site
Hello, Michael H.
Thanks for your kindly explanation.
Hope this can help others in the future.😄
This comment was minimized by the moderator on the site
Thank you for this great article.

For people who are using the array formular in non-English Excel there must be taken special care of the text format string: in your example: "R0C00".
For German this would translate to "Z0S00". However, "S" is a special character refering to seconds for time formating. This character needs to be escaped and therefore the correct format string for German Excel is "Z0\S00".

I hope this helps someone in the future :-)
This comment was minimized by the moderator on the site
Hello Sir! The VBA worked wonders, thank you very much for that! I was wondering, If I change the original data, is it possible to refresh the column with the unique values automatically?
Rated 5 out of 5
This comment was minimized by the moderator on the site
Hello Ioannis,

Glad to help. After you change the original data, the VBA can not refresh the result automatically. And the easiest way I can think of is to press Ctrl + Alt + F9 to refresh all results in worksheets in all open workbooks. Have a nice day.

Sincerely,
Mandy
This comment was minimized by the moderator on the site
The array formula at the top is working great when used with data in the same sheet, however when I try to use it to reference the same exact data from another sheet the formula returns nothing. I'm unable to figure out why. Is there a limitation with array functions that prevents you from referencing ranges in a different sheet?

Thanks for any insight you can provide.
This comment was minimized by the moderator on the site
Hello Erin,

Glad to help. The INDIRECT function in this formula is more complicated to use when referencing data in other worksheets. It is not recommended to use this feature when referencing ranges in different worksheets.

For example: Now the data is in Sheet1, I want to reference the content of cell C2 of Sheet1 in Sheet2. First, in any two cells in Sheet2, such as D1 and D2, enter Sheet1 and C2, respectively. At this point, enter the formula in the empty cell of Sheet2:
=INDIRECT("'"&D1&"'!"&D2), then the content of cell C2 in Sheet1 can be returned.

As you can see, it make things way more complex. Hope my explanation can help. Have a nice day.

Sincerely,
Mandy
This comment was minimized by the moderator on the site
Czy to żart?
This comment was minimized by the moderator on the site
can we create uniqdata function instead of macro?
This comment was minimized by the moderator on the site
Hi, İlhan,If you like a User Defined Function to create a formula for solving this problem, the below code may help you:After inserting the code, select a list of cells where you want to put the results. Then type this formula:=Uniques(A1:C4)  in the formula bar.Press Ctrl+Shift+Enter keys together. 


Function Uniques(ByVal inputRange As Range)
Dim inputArray As Variant
Dim myColl As New Collection
Dim xVal As Variant
Dim outArray() As Variant
On Error Resume Next
With inputRange
inputArray = Application.Intersect(.Cells, .Parent.UsedRange).Value
End With
On Error GoTo 0
On Error Resume Next
For Each xVal In inputArray
myColl.*** Item:=xVal, Key:=(CStr(xVal) & TypeName(xVal))
Next xVal
myColl.Remove "String"
On Error GoTo 0
ReDim outArray(1 To Application.Max(myColl.Count, Application.Caller.Cells.Count))
For xVal = 1 To UBound(outArray)
outArray(xVal) = vbNullString
Next xVal
For xVal = 1 To Application.Min(myColl.Count, Application.Caller.Cells.Count)
outArray(xVal) = myColl(xVal)
Next xVal
If Application.Caller.Columns.Count = 1 Then
Uniques = Application.Transpose(outArray)
Else
Uniques = outArray
End If
End Function
This comment was minimized by the moderator on the site
Thanks for the code. I'm using the VBA code of this page. Is there a way to add a sorting code after the unique values are extracted so it sorts it automatically?
This comment was minimized by the moderator on the site
Regarding the formula version, could you explain in more detail what this portion is doing? *100+COLUMN($A:$C),7^8)),"R0C00") Specifically, what are the *100, 7^8, and "R0C000" doing? I'm understanding everything else, but I can't figure out what these are for.
This comment was minimized by the moderator on the site
Little late for my response here but...
ROW($2:$9)*100 - this is multiplying the row number *100, so if it's in row 5, now the number is 500
COLUMN($A:$C) - this gets added to the row*100 number, so if it's row 5 col 2, then the number is 502.
7^8)), - this (I think) is to have a max value for the min statement from earlier.
"R0C00") - this formats the text based on the number. In the example, we had 502 so this gives R5C02 (row 5, col 02).

If you have a lot of columns but not many rows, then you could change it to ROW($2:$9)*1000+COLUMN($A:$C),7^8)),"R0C000")
This comment was minimized by the moderator on the site
i've adjusted to my sheet but am only returning the first value in the defined array... what am i missing?
This comment was minimized by the moderator on the site
Hello, Cody,
The above formula works well in my worksheet, could you give a screenshot of your data problem here?
Thank you!
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations