+ Reply to Thread
Results 1 to 3 of 3

Thread: Highlighting matching parts in two strings

  1. #1
    Forum Contributor ianh's Avatar
    Join Date
    11-19-2010
    Location
    Newcastle
    MS-Off Ver
    Excel 2010
    Posts
    111

    Highlighting matching parts in two strings

    I'm currently using the following function to compare two cells containing strings. It highlights any parts of the string that don't match.
    I'd like to be able to make it hightlight parts that do match but dont have the required skill (this function wasa found in a different post onhere)

    Function StringCompare(r1 As Range, r2 As Range) As Boolean
    Dim oMatches As Object, oMatch As Object
    Dim r(1 To 2) As Range
    Dim i As Integer, bDiff As Boolean
     
    Set r(1) = r1
    Set r(2) = r2
     
    With CreateObject("vbscript.regexp")
        .Pattern = "\W(\w+)(?=\W)(?=.*\|)(?!.*\|*\1)"
        .Global = True
        .IgnoreCase = True
            
        For i = 1 To 2
            Set oMatches = .Execute("," & r(i).Text & "|" & r(3 - i).Text)
            For Each oMatch In oMatches
                With r(i).Characters(Start:=oMatch.FirstIndex + 1, Length:=oMatch.Length - 1).Font
                    .Bold = True
                    .Size = 9
                    .color = RGB(255, 0, 0)
    
                End With
            Next oMatch
            If oMatches.Count > 0 Then bDiff = True
        Next i
    End With
    StringCompareHighlight = Not bDiff
    End Function
    Any help would be greatly appreciated.
    Last edited by ianh; 11-19-2010 at 06:46 AM.

  2. #2
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Highlighting matching parts in two strings

    Hi ianh
    Welcome to the Forum

    Please wrap your code in code tags, before the moderators get you...

    Forum rules
    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # button at the top of the post window. If you are editing an existing post, press Go Advanced to see the # button.

    Cheers

  3. #3
    Forum Contributor ianh's Avatar
    Join Date
    11-19-2010
    Location
    Newcastle
    MS-Off Ver
    Excel 2010
    Posts
    111

    Re: Highlighting matching parts in two strings

    Sorry, not a good start :-(

+ 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.2.0