+ Reply to Thread
Results 1 to 9 of 9

Compare columns and return matching text VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    02-16-2017
    Location
    New York, NY
    MS-Off Ver
    MS Office 2010
    Posts
    4

    Compare columns and return matching text VBA

    Hello,

    I'm working on comparing a large string of text against another string of text and outputting any matching values in another column. I've only been able to compare the two and return whether it's a match or not or even highlight the differences (not exactly accurate). Another idiosyncrasy is that each cell contains a listing of individual names which are concatenated and separated by a delimiter (semicolon in this case), I'd like to avoid parsing these out individually, as my end goal is to have any matching names in that format separated by a semicolon.


    Column 1: Jack Johnson;Tom Jackson;Josh Hazard;Tom Petty;John Kowalski
    Column 2: Tom Petty;Charles Smith;John Terry;Steven Gerrard;Tom Jones;Nick Jones;Michael Schumacher;George Willis;Donald Simson;Jack Johnson
    Output Column:Tom Petty;Jack Johnson

    Another issue I've ran into is that there is overlap in some of the names (e.g., the Jack in Tom Jackson comes back as a match for the Jack in Jack Johnson). Is there any way to treat each name individually based on the delimiter?

    Any help or suggestions would be greatly appreciated.

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Compare columns and return matching text VBA

    Hi alekos,

    Give this a try:

    Sub compare()
    'compares 2 lists by adding them to an array and then outputs a list of common and unique values
    Dim Arr As Variant
    Dim vArr As Variant
    Dim wss As Worksheet
    Dim x, y
    Dim match As Boolean
    Dim j As Long
    
    Application.ScreenUpdating = False
    
    Set wss = ThisWorkbook.Worksheets("Sheet1") 'sheet with source data
    'get the values in columns A and B into arrays for comparison, adjust the column letters if needed
    Arr = wss.Range("A2:A" & wss.Range("A" & Rows.Count).End(xlUp).Row).Value 'starts at A2 assuming a header
    vArr = wss.Range("B2:B" & wss.Range("B" & Rows.Count).End(xlUp).Row).Value 'starts at B2 assuming a header
    j = 1 'will start the list of values that are in both lists on row 2 because later we add 1 before writing
    'here we compare lists, output what is common to both
    For Each x In Arr
        match = False
        For Each y In vArr
            If x = y Then 'there is a match in col A vs Col B
                match = True
                j = j + 1
                wss.Range("C" & j).Value = x 'write the value of X in the list of common values.  "C" is the output column
            End If
        Next y
    Next x
    
    Application.ScreenUpdating = True
    
    End Sub
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Compare columns and return matching text VBA

    oops... just realized it is not necessarily a list... could use Split function instead... a sample sheet would be ideal

  4. #4
    Registered User
    Join Date
    02-16-2017
    Location
    New York, NY
    MS-Off Ver
    MS Office 2010
    Posts
    4

    Re: Compare columns and return matching text VBA

    Please see attached.
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,466

    Re: Compare columns and return matching text VBA

    try this WORK FOR ME
    Sub tess()
    Dim r As Range, dic As Object, t$, k, i&, sp, ii&, sp1$, lr&
    Set dic = CreateObject("scripting.dictionary")
    With Worksheets("Example")
        lr = .UsedRange.SpecialCells(11).Row
        Set r = .Range("b2:D" & lr)
          x = r.Value
          For i = 1 To UBound(x)
             t = x(i, 1) & ";" & x(i, 2)
             sp = Split(t, ";")
              For ii = 0 To UBound(sp)
                 If Not dic.exists(Trim$(sp(ii))) Then
                    dic(sp(ii)) = sp(ii)
                 Else
                    dic(sp(ii)) = dic(Trim$(sp(ii))) & "," & Trim$(sp(ii))
                 End If
              Next ii
             For Each k In dic.keys
                If InStr(dic(k), ",") Then
                  sp1 = IIf(sp1 = "", k, sp1 & ";" & k)
                End If
             Next k
              x(i, 3) = sp1
              sp1 = "": sp = "": dic.RemoveAll
          Next i
            .[d2].Resize(lr - 1) = Application.Index(x, 0, 3)
     End With
    End Sub
    Last edited by daboho; 11-09-2018 at 01:48 PM.
    "Presh Star Who has help you *For Add Reputation!! And mark case as Solve"

  6. #6
    Registered User
    Join Date
    02-16-2017
    Location
    New York, NY
    MS-Off Ver
    MS Office 2010
    Posts
    4

    Re: Compare columns and return matching text VBA

    This is brilliant! I tried setting it up as a loop (my actual data set is exponentially larger), but I'm getting a subscript out of range error. I need this to run 68 times across the set of 2 columns & output in the 3rd column each time (columns are all next to each other). Is there any way to set it up to run a loop through those instead of defining each specific range?

    Set r = .Range("b2:D" & lr)
    Set r = .Range("e2:G" & lr)
    Set r = .Range("h2:J" & lr)
    .
    .
    .
    Set r = .Range("gu2:GW" & lr)

  7. #7
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,466
    Quote Originally Posted by alekos7 View Post
    This is brilliant! I tried setting it up as a loop (my actual data set is exponentially larger), but I'm getting a subscript out of range error. I need this to run 68 times across the set of 2 columns & output in the 3rd column each time (columns are all next to each other). Is there any way to set it up to run a loop through those instead of defining each specific range?

    Set r = .Range("b2:D" & lr)
    Set r = .Range("e2:G" & lr)
    Set r = .Range("h2:J" & lr)
    .
    .
    .
    Set r = .Range("gu2:GW" & lr)
    You just add range array
    Dim r(1 to 3)
    Set r(1) =.range("b2:d" & lr)
    Set r(2) = .range("e2:g" & lr)
    Set r(3) = .range("h2:j" & lr)
    'etc
    'you can loop
    For rw = 1 to 3
       x = r(rw).value
       'add my code to this
        
    
    
    Next rw
    Or if your range is have sama space with other you can loop

    For myrange = 1 to 3 'asumsi 3 areas range
       With sheets("Example")
       x= .range(b2).resize(.usedrange.specialcell(11).row,3)
       'add my code above to thid
       'small change to after next i
    
       '.[d2].resize(ubound(x)) = x
       'change to
       .columns(Ubound(x,2)).offset(1) = x
    
       j = j + 4 'for move to next range

  8. #8
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Compare columns and return matching text VBA

    How about:

    Sub compare()
    'compares 2 lists by adding them to an array and then outputs a list of common and unique values
    Dim Arr As Variant
    Dim vArr As Variant
    Dim wss As Worksheet
    Dim x, y
    Dim match As Boolean
    Dim j As Long
    Dim i As Long
    Dim lr As Long
    Dim namesList As String
    
    Application.ScreenUpdating = False
    
    Set wss = ThisWorkbook.Worksheets("Example") 'sheet with source data
    
    lr = wss.Range("B" & Rows.Count).End(xlUp).Row 'last row with data in Col B
    
    For i = 2 To lr
        'get the values in columns A and B into arrays for comparison, adjust the column letters if needed
        Arr = Split(wss.Range("B" & i).Value, ";")
        vArr = Split(wss.Range("C" & i).Value, ";")
        namesList = vbNullString
        match = False
        'here we compare lists, output what is common to both
        For Each x In Arr
            For Each y In vArr
                If x = y Then 'there is a match in col A vs Col B
                    match = True
                    namesList = namesList & x & ";"
                End If
            Next y
        Next x
        If match = True Then
            'remove trailing ";"
            namesList = Left(namesList, Len(namesList) - 1)
            wss.Range("D" & i).Value = namesList
        End If
    Next i
    Application.ScreenUpdating = True
    
    End Sub

  9. #9
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,466

    Re: Compare columns and return matching text VBA

    And perhab you can change to this
    I am sorri not test
    Or another idea
    'b,e,h
    Sub xxx (r as range,sh as string)
    Dim dic As Object, t$, k, i&, sp, ii&, sp1$, lr&
    Set dic = CreateObject("scripting.dictionary")
    Sheets(sh).Activate
          x = r.Value
          For i = 1 To UBound(x)
             t = x(i, 1) & ";" & x(i, 2)
             sp = Split(t, ";")
              For ii = 0 To UBound(sp)
                 If Not dic.exists(Trim$(sp(ii))) Then
                    dic(sp(ii)) = sp(ii)
                 Else
                    dic(sp(ii)) = dic(Trim$(sp(ii))) & "," & Trim$(sp(ii))
                 End If
              Next ii
             For Each k In dic.keys
                If InStr(dic(k), ",") Then
                  sp1 = IIf(sp1 = "", k, sp1 & ";" & k)
                End If
             Next k
              x(i, 3) = sp1
              sp1 = "": sp = "": dic.RemoveAll
          Next i
           r.columns(3).offset(1).Resize(r.rows
    Count-1) = Application.Index(x, 0, 3)
    End Sub
    In other code you can call
    Sub tesss()
    Dim lr as long
    lr = activesheet.usedrange.specialcells(11).row
    xxx Sheets("Example").range("b2:d" & lr),"Example"
    
    'second range
    xxx sheets("Example").range("e2:g" & lr),"Example"
    
    'third range etc

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How to compare text of same row in two different columns and return matching words?
    By guest2013 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-17-2015, 03:22 PM
  2. Compare columns and finding matching values/text
    By debake in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-02-2014, 09:27 AM
  3. Replies: 0
    Last Post: 10-07-2013, 10:24 AM
  4. Replies: 2
    Last Post: 07-17-2013, 11:48 AM
  5. Replies: 1
    Last Post: 04-21-2013, 08:36 AM
  6. Replies: 6
    Last Post: 02-28-2013, 11:27 AM
  7. [SOLVED] Compare cells/columns and highlight matching text strings
    By luxbelle in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-25-2005, 06:06 PM

Tags for this Thread

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