Excel compare two columns and highlight duplicates

前端 未结 8 739
春和景丽
春和景丽 2021-01-30 02:45

I have an excel file with 10,000 rows in column A some values are the same.

Example:

A1 - P7767

A2 - P3443

A3 - P7767

A4 - P8746

A5 - P9435
         


        
8条回答
  •  清歌不尽
    2021-01-30 03:02

    I was trying to compare A-B columns and highlight equal text, but usinng the obove fomrulas some text did not match at all. So I used form (VBA macro to compare two columns and color highlight cell differences) codes and I modified few things to adapt it to my application and find any desired column (just by clicking it). In my case, I use large and different numbers of rows on each column. Hope this helps:

    Sub ABTextCompare()

    Dim Report As Worksheet
    Dim i, j, colNum, vMatch As Integer
    Dim lastRowA, lastRowB, lastRow, lastColumn As Integer
    Dim ColumnUsage As String
    Dim colA, colB, colC As String
    Dim A, B, C As Variant
    
    Set Report = Excel.ActiveSheet
    vMatch = 1
    
    'Select A and B Columns to compare
    On Error Resume Next
     Set A = Application.InputBox(Prompt:="Select column to compare", Title:="Column A", Type:=8)
      If A Is Nothing Then Exit Sub
    colA = Split(A(1).Address(1, 0), "$")(0)
     Set B = Application.InputBox(Prompt:="Select column being searched", Title:="Column B", Type:=8)
       If A Is Nothing Then Exit Sub
      colB = Split(B(1).Address(1, 0), "$")(0)
     'Select Column to show results
     Set C = Application.InputBox("Select column  to show results", "Results", Type:=8)
        If C Is Nothing Then Exit Sub
      colC = Split(C(1).Address(1, 0), "$")(0)
    
    'Get Last Row
    lastRowA = Report.Cells.Find("", Range(colA & 1), xlFormulas, xlByRows, xlPrevious).Row - 1 ' Last row in column A
    lastRowB = Report.Cells.Find("", Range(colB & 1), xlFormulas, xlByRows, xlPrevious).Row - 1 ' Last row in column B
    
     Application.ScreenUpdating = False
    '***************************************************
    For i = 2 To lastRowA
          For j = 2 To lastRowB
              If Report.Cells(i, A.Column).Value <> "" Then
                  If InStr(1, Report.Cells(j, B.Column).Value, Report.Cells(i, A.Column).Value, vbTextCompare) > 0 Then
                      vMatch = vMatch + 1
                      Report.Cells(i, A.Column).Interior.ColorIndex = 35 'Light green background
                      Range(colC & 1).Value = "Items Found"
                      Report.Cells(i, A.Column).Copy Destination:=Range(colC & vMatch)
                      Exit For
                  Else
                      'Do Nothing
                  End If
              End If
          Next j
      Next i
    If vMatch = 1 Then
        MsgBox Prompt:="No Itmes Found", Buttons:=vbInformation
    End If
    '***************************************************
    Application.ScreenUpdating = True
    

    End Sub

提交回复
热议问题