今天项⽬组的⼀个同事问我如何快速的找到⼀个Excel中第3列和第5列的值完全重复的值,我想了想虽然Excel中⾃带查找重复值的功能,但是好像只能对同⼀列进⾏⽐较,所以就写了⼀个VBA进⾏处理,VBA⾮常简单,但效果不错。Sub FindDuplicatesInColumn()
Dim lastRow As Long
Dim matchFoundIndex As Long Dim iCntr As Long lastRow = 500
' 初始化临时列, 第7列⽤来存放结果,第8列将3 5两列的值拼接起来,⽅便判断 For iCntr = 2 To lastRow Cells(iCntr, 7) = \"\"
Cells(iCntr, 8) = Cells(iCntr, 3) & Cells(iCntr, 5) Next iCntr
For iCntr = 2 To lastRow
If Cells(iCntr, 5) <> \"\" Then
' 判断是否存在相等的拼接后的列,如果存在,则记录到结果中
matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 8), Range(\"H1:H\" & lastRow), 0) If iCntr <> matchFoundIndex Then
Cells(iCntr, 7) = \"Duplicate with \" & matchFoundIndex End If End If Next iCntrEnd Sub
因篇幅问题不能全部显示,请点此查看更多更全内容