EXCEL VBA 比较 2 列并删除重复的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28023684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EXCEL VBA TO COMPARE 2 COLUMNS & DELETE DUPLICATED ROWS
提问by ZRS
I need to compare the identical values in 2 columns and delete the duplicated rows decribed below:
我需要比较 2 列中的相同值并删除下面描述的重复行:
A B
1 5 A
2 6 B
3 7 C
4 5 A
5 6 A
6 7 C
The rows 4 & 6 in 2 columns are identical and must be deleted after running of macro. I searched on internet but could not find to delete the right rows with true mathching. The sheet after vba should be:
2 列中的第 4 行和第 6 行是相同的,必须在运行宏后删除。我在互联网上搜索,但找不到用真正的数学来删除正确的行。vba 之后的工作表应该是:
A B
1 5 A
2 6 B
3 7 C
4 6 A
Thanks in advance
提前致谢
回答by Petay87
Unless I am missing something, you don't need to use VBA to perform this action.
除非我遗漏了什么,否则您不需要使用 VBA 来执行此操作。
You can use the "Remove Duplicates" function from the "Data" tab.
您可以使用“数据”选项卡中的“删除重复项”功能。
Example:
例子:
I have the following:
我有以下几点:
You can see that rows 13,14,15, & 16 are duplicates when you compare both Columns A & B.
当您比较 A 列和 B 列时,您可以看到第 13、14、15 和 16 行是重复的。
Solution:
解决方案:
Highlight BOTH ENTIRE Columns A & B then run the "Remove Duplicates" function. You will be prompted with something Similar to this depending on your headers.
突出显示 BOTH ENTIRE Columns A & B,然后运行“Remove Duplicates”功能。根据您的标题,您将收到与此类似的提示。
Ensure you have BOTH columns selected and click on the OK button.
确保您选择了 BOTH 列并单击 OK 按钮。
My outcome was the following:
我的结果如下:
If you really need this to be done in VBA then I got the following code when running the Macro Recorder:
如果您真的需要在 VBA 中完成此操作,那么在运行宏记录器时我会得到以下代码:
Sub Macro1()
Columns("A:B").Select
ActiveSheet.Range("$A:$B").RemoveDuplicates Columns:=Array(1, 2), Header _
:=xlNo
End Sub
You should be able to edit that to fit your needs.
您应该能够编辑它以满足您的需要。