我的代码突出显示了两列中的重复值,基于行的不同为两个。如果更改列顺序,则无法找到重复的值。建议?
Public Sub CompareColumns(sourceCol As String, targetCol As String, sourceExcelFile As String, targetExcelFile As String)
Try
excelApp = New Excel.Application()
excelApp.Visible = False
excelWorkbook = excelApp.Workbooks.Open(sourceExcelFile)
excelWorkbook2 = excelApp.Workbooks.Open(targetExcelFile)
excelWorkSheet1 = excelWorkbook.ActiveSheet
excelWorkSheet2 = excelWorkbook2.ActiveSheet
lastLineSource = excelWorkSheet1.UsedRange.Rows.Count
LastLineTarget = excelWorkSheet2.UsedRange.Rows.Count
For row As Integer = 1 To lastLineSource
If excelWorkSheet1.Range(sourceCol & row.ToString()).Value = excelWorkSheet2.Range(targetCol & row.ToString()).Value Then
excelWorkSheet2.Range(targetCol & row.ToString()).Interior.Color = 5296274
End If
Next
excelWorkbook.Save()
excelWorkbook2.Save()
excelWorkbook.Close()
excelWorkbook2.Close()
excelApp.Quit()
Catch
Throw
End Try
End Sub
预期输出是要突出显示源工作簿中目标工作簿中所有重复值,而不考虑列顺序(排序)
答案 0 :(得分:0)
所以我认为您应该使用字典: https://www.dotnetperls.com/dictionary-vbnet
您可以循环浏览第一个工作簿中的所有值,并将其添加到字典中。 然后,您应该遍历第二个工作簿中的所有单元格,如果您在字典中找到值,则您有重复项,如果没有,则没有重复项。
不管顺序(排序)如何,它都可以工作,而且速度很快。