+ Reply to Thread
Results 1 to 5 of 5

Excel Macro or Functions to Read Mismatch string between two cells

Hybrid View

venkateswarank Excel Macro or Functions to... 09-29-2012, 10:22 AM
JBeaucaire Re: Excel Macro or Functions... 09-29-2012, 11:27 AM
venkateswarank Re: Excel Macro or Functions... 09-29-2012, 01:21 PM
patel45 Re: Excel Macro or Functions... 09-29-2012, 11:54 AM
JBeaucaire Re: Excel Macro or Functions... 09-29-2012, 02:11 PM
  1. #1
    Registered User
    Join Date
    09-29-2012
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    2

    Excel Macro or Functions to Read Mismatch string between two cells

    I need help to read mismatch string between two cells it may custom UDF (User Defined Function) or Macro to find.

    For Ex. Input is A1 = C1,C2,C3,C5 and B1=C1,C4,C2
    Expected O/P is C1 = C3,C5,C4


    Regards,
    Venkateswaran K

  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: Excel Macro or Functions to Read Mismatch string between two cells

    Here's the UDF for you:
    Option Explicit
    
    Function CompareLists(Rng1 As Range, Rng2 As Range, Optional Delim As String) As String
    Dim Arr As Variant, buf As String, n As Long
    
    If Delim = "" Then Delim = ","
    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
    
    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
    
    If Len(buf) > 0 Then CompareLists = Mid(buf, 2, Len(buf)) Else CompareLists = "All match"
    
    End Function
    Put it into a standard module (Insert > Module), then use in a cell like so:

    =CompareLists(A1, B1, ",")

    You can use any delimiter you want in the 3rd paramater, if you leave it out it will default to comma.
    Last edited by JBeaucaire; 09-29-2012 at 12:15 PM.
    _________________
    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
    09-29-2012
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Excel Macro or Functions to Read Mismatch string between two cells

    Excellent Thanks its solved my problem.

    Regards,
    Venkateswaran K



    MODERATOR'S NOTE: Please do not quote entire posts into your posts. Thanks.
    Last edited by JBeaucaire; 09-29-2012 at 02:11 PM. Reason: Removed unnecessary whole-post quotation.

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Excel Macro or Functions to Read Mismatch string between two cells

    Function GetDiff(txt1 As String, txt2 As String) As String
        Dim e, a(1 To 3) As String
        Static dic As Object
        If dic Is Nothing Then Set dic = CreateObject("Scripting.Dictionary")
        dic.CompareMode = 1
        dic.RemoveAll
        For Each e In Split(txt1, ",")
            If Trim$(e) <> "" Then
                dic(Trim$(e)) = Empty
            End If
        Next
        For Each e In Split(txt2, ",")
            If Trim$(e) <> "" Then
                If dic.exists(Trim$(e)) Then
                    a(2) = a(2) & IIf(a(2) = "", "", ", ") & Trim$(e)
                    dic.Remove Trim$(e)
                Else
                    a(3) = a(3) & IIf(a(3) = "", "", ", ") & Trim$(e)
                End If
            End If
        Next
        a(1) = Join$(dic.keys, ", ") & ", " & a(3)
        GetDiff = a(1)
    End Function
    don't ask me comments, it's not mine
    Last edited by patel45; 09-29-2012 at 12:21 PM.
    If solved remember to mark Thread as solved

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

    Re: Excel Macro or Functions to Read Mismatch string between two cells

    I have marked this thread solved for you.
    In the future please select Thread Tools from the menu above and mark the thread as solved. Thanks.

+ 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