Applies ToMicrosoft 365 专属 Excel Microsoft 365 Mac 版专属 Excel Excel 2024 Excel 2024 for Mac Excel 2021 Excel 2021 for Mac Excel 2019 Excel 2016 Office 商业版 iPhone 版我的 Office Office.com

可以使用以下方法比较两个 Microsoft Excel 工作表列中的数据并查找重复条目。 

方法 1:使用工作表公式

  1. 启动 Excel。

  2. 在新工作表中,输入以下数据作为示例(将 B 列留空):

    A

    B

    C

    1

    1

    3

    2

    2

    5

    3

    3

    8

    4

    4

    2

    5

    5

    0

  3. 在单元格 B1 中键入以下公式: =IF(ISERROR(MATCH(A1,$C$1:$C$5,0)),"",A1)

  4. 选择单元格 B1 到 B5。

  5. “编辑”组中选择“填充”,然后选择“向下”。 重复数字会显示在 B 列中,如以下示例所示: 

    A

    B

    C

    1

    1

    3

    2

    2

    2

    5

    3

    3

    3

    8

    4

    4

    2

    5

    5

    5

    0

方法 2:使用 Visual Basic 宏

警告: Microsoft 的编程示例仅用于说明,不做任何明示或暗示的保证。 这包括但不限于特定用途的适销性或适用性的隐含保证。 本文假定你熟悉所演示的编程语言以及用于创建和调试过程的工具。 Microsoft 支持工程师可以帮助解释特定过程的功能。 但他们不会修改这些示例以提供附加功能或构造满足你的特定要求的过程。

若要使用 Visual Basic 宏比较两列中的数据,请使用以下示例中的步骤:

  1. 启动 Excel。

  2. 按 ALT+F11 启动 Visual Basic 编辑器。

  3. 在“插入”菜单上,选择“加载项”。

  4. 在模块工作表中输入下面的代码:

    Sub Find_Matches()
    Dim CompareRange As Variant, x As Variant, y As Variant
    ' Set CompareRange equal to the range to which you will
    ' compare the selection.
    Set CompareRange = Range("C1:C5")
    ' NOTE: If the compare range is located on another workbook
    ' or worksheet, use the following syntax.
    ' Set CompareRange = Workbooks("Book2"). _
    ' Worksheets("Sheet2").Range("C1:C5")
    '
    ' Loop through each cell in the selection and compare it to
    ' each cell in CompareRange.
    For Each x In Selection
    For Each y In CompareRange
    If x = y Then x.Offset(0, 1) = x
    Next y
    Next x
    End Sub
  5. 按 ALT+F11 返回到 Excel。

    1. 输入以下数据作为示例(将 B 列留空):  

      A

      B

      C

      1

      1

      3

      2

      2

      5

      3

      3

      8

      4

      4

      2

      5

      5

      0

  6. 选择 A1 到 A5。

  7. 选择“开发工具”选项卡,然后在“代码”组中选择“”。

    注意: 如果看不到“开发工具”选项卡,则可能需要将其打开。 为此,请选择“文件”>“选项”>“自定义功能区”,然后在右侧的自定义框中选择“开发工具”选项卡。

  8. 单击“Find_Matches”,然后单击“运行”。 重复数字显示在 B 列。匹配的数字将放在第一列旁边,如下所示:

    A

    B

    C

    1

    1

    3

    2

    2

    2

    5

    3

    3

    3

    8

    4

    4

    2

    5

    5

    5

    0

需要更多帮助?

需要更多选项?

了解订阅权益、浏览培训课程、了解如何保护设备等。