【问题标题】:Find duplicates in two columns in two sheets在两张纸的两列中查找重复项
【发布时间】:2016-03-02 20:05:53
【问题描述】:

我需要比较两个不同工作表中的两列并找出重复项:将“Sheet2”中的 C 列与“Sheet1”中的 E 列进行比较。作为示例,我只使用了几个,但是:

Column E in Sheet1 has 2,000 rows of data.  
Column C in Sheet2 has ~ 100 rows of data.

表格截图:http://postimg.org/image/jtuinkqgz/

Sheet1

Sheeet2

链接到sample Excel file

Option Explicit
Sub CompareColumns()
'---------------------------------------------------------------------------------------------------
This module loops through two columns in Excel and identifies items without a match. The columns can be on different sheets. -----------
'---------------------------------------------------------------------------------------------------
Dim strCol1 As String 'First Column Location
Dim strCol2 As String 'Second Column Location
Dim strColResults As String 'Output Column
Dim strSheetname1 As String 'First sheet name
Dim strSheetname2 As String 'Second sheet name
Dim iListStart As Integer 'Row where List Begins
Dim strTemp As String
Dim i As Integer, j As Integer
Dim iLastRow1 As Integer, iLastRow2 As Integer

'---Edit these variables---'
strSheetname1 = "Sheet1"
strSheetname2 = "Sheet2"
strCol1 = "A"
strCol2 = "C"
'strColResults = "B"
iListStart = 1
'--------------------------'

iLastRow1 = Sheets(strSheetname1).Range(strCol1 & "50000").End(xlUp).Row
iLastRow2 = Sheets(strSheetname2).Range(strCol2 & "50000").End(xlUp).Row

'error check
If iListStart > WorksheetFunction.Min(iLastRow1, iLastRow2) Then
MsgBox ("List not found. Perform logic check on input variables.")
Exit Sub
End If

Sheets(strSheetname1).Range(strCol1 & iListStart & ":" & strCol1 & iLastRow1).Interior.ColorIndex = 0
Sheets(strSheetname2).Range(strCol2 & iListStart & ":" & strCol2 & iLastRow2).Interior.ColorIndex = 0

strTemp = "<<"
If iLastRow2 > iLastRow1 Then 'switch the order
strTemp = strCol1
strCol1 = strCol2
strCol2 = strTemp
strTemp = strSheetname1
strSheetname1 = strSheetname2
strSheetname2 = strTemp
strTemp = ">>"
End If

'Identify unmatched items in long column
For i = iListStart To WorksheetFunction.Max(iLastRow1, iLastRow2)
For j = iListStart To WorksheetFunction.Min(iLastRow1, iLastRow2)
If UCase(Sheets(strSheetname2).Range(strCol2 & j)) =          UCase(Sheets(strSheetname1).Range(strCol1 & i)) Then
'Range(strColResults & i) = i & " to " & j
Exit For ' Stops at first match
ElseIf j = WorksheetFunction.Min(iLastRow1, iLastRow2) Then
'Range(strColResults & i) = strTemp
Sheets(strSheetname1).Range(strCol1 & i).Interior.Color = 255
End If
Next j
Next i

'Identify unmatched items in short column
If strTemp = "<<" Then
strTemp = " >>"
Else
strTemp = " <<"
End If

For i = iListStart To WorksheetFunction.Min(iLastRow1, iLastRow2)
For j = iListStart To WorksheetFunction.Max(iLastRow1, iLastRow2)
If UCase(Sheets(strSheetname1).Range(strCol1 & j)) =             UCase(Sheets(strSheetname2).Range(strCol2 & i)) Then
Exit For
ElseIf j = WorksheetFunction.Max(iLastRow1, iLastRow2) Then
'Range(strColResults & i) = Range(strColResults & i) & strTemp
Sheets(strSheetname2).Range(strCol2 & i).Interior.Color = 255
End If
Next j
Next i
End Sub

如何在 C 列中找到重复项,并用 Sheet1 的 E 列将 Sheet2 的 C 列的重复项着色?

【问题讨论】:

    标签: excel excel-formula conditional-formatting countif vba


    【解决方案1】:

    如果你想要一个 VBA 解决方案,你可以使用这个:

    Sub test()
    
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim lR1&, lR2
    
    Dim rng As Range, Frng As Range
    
    Set ws1 = Worksheets("Sheet1") 'Change this to whatever sheet name
    Set ws2 = Worksheets("Sheet2") 'Same as above
    
    With ws1
    
        lR1 = .Cells(.Rows.Count, 3).End(xlUp).Row
    
    End With
    
    With ws2
    
        lR2 = .Cells(.Rows.Count, 5).End(xlUp).Row
    
    End With
    
    For Each rng In ws1.Range("C1", "C" & lR1) 'Edit this to the range where the values are located you want to find the duplicates of
    
        Set Frng = ws2.Range("E1", "E" & lR2).Find(what:=rng.Value, LookIn:=xlValues, LookAt:=xlWhole, _
                                            MatchCase:=False, SearchFormat:=False)
    
        If Not Frng Is Nothing Then
    
            Frng.Interior.Color = 255
    
        End If
    
    Next rng
    
    End Sub
    

    但是请注意,这是区分大小写的。

    【讨论】:

      【解决方案2】:

      似乎不是这个 OP 想要的,但对于具有非常相似要求的其他人来说似乎是一个不错的选择:在 Sheet2 和 HOME > Styles - Conditional Formatting, New Rule... 中选择 ColumnC,使用公式确定要格式化的单元格格式化此公式为真的值:

      =COUNTIF(Sheet1!E:E,C1)
      

      Format...,选择你选择的格式,OK,OK。

      【讨论】:

        猜你喜欢
        • 2019-04-25
        • 1970-01-01
        • 2020-02-05
        • 1970-01-01
        • 1970-01-01
        • 2016-02-27
        • 2021-06-13
        • 2019-11-24
        • 2018-12-02
        相关资源
        最近更新 更多