Results 1 to 8 of 8

Compare 2 columns and return the best match

Threaded View

  1. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Compare 2 columns and return the best match

    Try next code and comment
    Option Explicit
    Option Base 1
    
    Sub PrepareData()
    Dim ObjDic    As Object
    Dim NbRef As Object
    Dim REF()
    Dim LastRow1   As Long, LastRow2   As Long
    Dim I  As Long, J As Long, K As Long
    Dim J_Ref As Long
    Dim TEMP
    
    Set ObjDic = CreateObject("Scripting.Dictionary")
    
        LastRow2 = Range("C" & Rows.Count).End(xlUp).Row
        ReDim REF(1 To LastRow2, 1 To 2)
        For I = 2 To LastRow2
            TEMP = Split(Cells(I, "C"), " ")
            If (UBound(REF, 2) < UBound(TEMP, 1) + 1) Then
               ReDim Preserve REF(1 To UBound(REF, 1), 1 To UBound(TEMP, 1) + 1)
            End If
            For J = 0 To UBound(TEMP, 1)
                REF(I, J + 1) = TEMP(J)
            Next J
        Next I
        LastRow1 = Range("A" & Rows.Count).End(xlUp).Row
        For I = 2 To LastRow1
            For J = 2 To LastRow2
                For K = 1 To UBound(REF, 2)
                    If ((REF(J, K) <> Empty) And (Len(REF(J, K)) > 1)) Then
                        If (Len(Cells(I, "A")) - Len(Replace(Cells(I, "A"), REF(J, K), "")) > 0) Then
                            If (ObjDic.exists(J)) Then
                                ObjDic.Item(J) = ObjDic.Item(J) + 1
                            Else
                                ObjDic.Add J, 1
                            End If
                            If (ObjDic.Item(J) = WorksheetFunction.Large(ObjDic.items, 1)) Then J_Ref = J
                        End If
                    End If
                Next K
            Next J
            Cells(I, "B") = Cells(J_Ref, "C")
            ObjDic.RemoveAll
        Next I
    End Sub
    Last edited by PCI; 12-27-2013 at 06:18 AM. Reason: Code updated

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 05-22-2013, 04:40 AM
  2. [SOLVED] Compare data in columns of two worksheets and return value if match.
    By Zoediak in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-02-2013, 10:38 AM
  3. Index/Match to return values to compare
    By Iptgfs in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-01-2013, 12:39 PM
  4. How to compare columns and return one value if they match
    By louise0502 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-16-2013, 11:10 AM
  5. Replies: 1
    Last Post: 10-04-2012, 02:31 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1