+ Reply to Thread
Results 1 to 4 of 4

URGENT find common string values in 2 colums

Hybrid View

  1. #1
    Registered User
    Join Date
    12-11-2012
    Location
    United Arab Emirates
    MS-Off Ver
    Excel 2010
    Posts
    2

    URGENT find common string values in 2 colums

    Dear All,

    Please help in the following...

    i have 2 columns each on have string values separated by commas
    example:

    columns 1

    O9,L2,A2,7E,IT2
    A2,C6,HY,IK,O9,CHE,MCP
    IK,3D,HY,A8,P0,IY
    7E,HY,O9,IT2,L2


    columns 2
    P0,L2,IT2,7E,3D
    A2,HY,L2,IK,O9,CHE,MCP
    IK,IT2,HY,A8,7E,O9
    O9,C6,IY,A2,HY

    I need to create a button to run a macro to create the 3rd column of Common string. The cells under the Common String column must contain the common codes from the first 2 columns and must be separated by comma in alphabetical order.

    Please anyone can you help.

    Regards

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: URGENT find common string values in 2 colums

    Click GO ADVANCED and use the paperclip icon to post up a copy of your workbook. Make sure the workbook demonstrates your sample data AND your desired results.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    12-11-2012
    Location
    United Arab Emirates
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: URGENT find common string values in 2 colums

    sample.xlsx

    please find attached sample,

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: URGENT find common string values in 2 colums

    Here are two functions to accomplish this. One I wrote called CompareLists() and it is a UDF. Here's the code, it goes in a standard code module, not a sheet module:
    Option Explicit
    
    Function CompareLists(Rng1 As Range, Rng2 As Range, _
        Optional Delim As String, Optional Incl As Boolean) As String
    'Compare the items in two delimited lists and make a list of items
    '  Incl=TRUE: in both lists (common)
    '  Incl=FALSE: NOT in both lists (uncommon)
    Dim Arr As Variant, buf As String, n As Long
    
    If Delim = "" Then Delim = ","
    If Incl Then
        Arr = Split(Rng1.Cells(1), Delim)
    
        For n = LBound(Arr) To UBound(Arr)
            If InStr(Rng2, Arr(n)) > 0 And Len(Arr(n)) > 0 Then buf = buf & "," & Arr(n)
        Next n
    Else
        Arr = Split(Rng1.Cells(1), Delim)
        If InStr(Rng2, Arr(n)) = 0 And Len(Arr(n)) > 0 Then buf = buf & "," & Arr(n)
        Arr = Split(Rng2.Cells(1), Delim)
        For n = LBound(Arr) To UBound(Arr)
            If InStr(Rng1, Arr(n)) = 0 And Len(Arr(n)) > 0 Then buf = buf & "," & Arr(n)
        Next n
    End If
    
    If Len(buf) > 0 Then
        buf = BubbleSort2(buf, Delim)
        CompareLists = Mid(buf, 2, Len(buf))
    Else
        CompareLists = "no results"
    End If
    
    End Function
    
    
    Function BubbleSort2(numString As String, Delim As String) As String
    Dim Arr As Variant, vTemp As Variant
    Dim i As Long, j As Long, buf As String
    
    Arr = Split(numString, Delim)
    
    'from John Walkenbach’s Excel Power Programming with VBA to sort the array
        For i = LBound(Arr, 1) To UBound(Arr, 1) - 1
            For j = i + 1 To UBound(Arr, 1)
                If Arr(i) > Arr(j) Then
                    vTemp = Arr(i)
                    Arr(i) = Arr(j)
                    Arr(j) = vTemp
                End If
            Next j
        Next i
    
    BubbleSort2 = WorksheetFunction.Transpose(Join(Arr, Delim))
    End Function

    Once installed you use it in a cell like any other formula, in C2:

    =COMPARELISTS(A2, B2, ",", TRUE)


    RANGE1 - the first delimited list in a cell. Only the first cell will be used if this is a multicell range
    RANGE2 - the second delimited list in another cell. Only the first cell will be used if this is a multicell range
    DELIMITER - an optional string that indicates the delimiter in each comparison cell, the default is comma
    TRUE/FALSE - optional, if TRUE is a list of common items, if FALSE or omitted is a list of non-common items



    The second function is a BubbleSort function using a popular technique that is used to "sort an array in vba", it is putting the results into alphabetical order as you requested.
    Last edited by JBeaucaire; 12-13-2012 at 11:25 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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