如何比较两列中的数据以在 Excel 中查找重复项

如何比较两列中的数据以在 Excel 中查找重复项

启动 Excel。

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

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

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

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

按 ALT+F11 返回到 Excel。

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

A

B

C

1

1

3

2

2

5

3

3

8

4

4

2

5

5

0

选择 A1 到 A5。

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

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

单击“Find_Matches”,然后单击“运行”。

重复数字显示在 B 列。匹配的数字将放在第一列旁边,如下所示:

A

B

C

1

1

3

2

2

2

5

3

3

3

8

4

4

2

5

5

5

0

相关推荐