How do I combine these two codes into one comparing macro?

jonathynblythe

New Member
Joined
Apr 14, 2011
Messages
4
Hello again...

Here's a code I have that I use for comparing two sheets in an excel workbook

Code:
Sub FindMismatch()
    Dim rInp        As Range    ' input range
    Dim cellI       As Range    ' cell in rInp
 
    Dim r2F         As Range    ' Sheet2 col F
    Dim cellF       As Range    ' cell in col F
 
    Dim vi          As Variant  ' for/each variable
 
    Set rInp = Application.InputBox("Choose the first range", "Range 1", Type:=8)
    If rInp Is Nothing Then Exit Sub
 
    With Worksheets("Sheet2")
        Set r2F = .Range("F1", .Cells(.Rows.Count, "F").End(xlUp))
    End With
 
    For Each cellI In rInp
        Select Case WorksheetFunction.CountIf(r2F, "*" & cellI.Text & "*")
            Case 0
                cellI.Interior.ColorIndex = 5
            Case 1
                For Each vi In Array(-5, -4, -3, -2, -1, 1)
                    For Each cellF In r2F
                        If cellF.Value = cellI.Value And cellF.Offset(0, vi).Value <> cellI.Offset(0, vi).Value Then
                            cellI.Offset(0, vi).Interior.ColorIndex = 3
                        End If
                    Next cellF
                Next vi
        End Select
    Next cellI
End Sub

I have another code from Bob Phillips over at excelguru.ca and he gave me this:

Code:
Public Sub ProcessData()
Dim cell As Range
Dim Lastrow As Long
Dim i As Long, j As Long

    Application.ScreenUpdating = False
    
    With Worksheets(1)
    
        Lastrow = .UsedRange.Rows.Count + .UsedRange.Cells(1, 1).Row - 1
        For i = 1 To Lastrow
        
            On Error Resume Next
                Set cell = Worksheets(2).Columns(1).Find(.Cells(i, "A").Value2)
            On Error GoTo 0
            If Not cell Is Nothing Then
            
                Call CheckValues(Worksheets(1), i, 2, cell)
                Call CheckValues(Worksheets(1), i, 3, cell)
                Call CheckValues(Worksheets(1), i, 4, cell)
                Call CheckValues(Worksheets(1), i, 5, cell)
                Call CheckValues(Worksheets(1), i, 6, cell)
            End If
        Next i
    End With
    
    Application.ScreenUpdating = True
End Sub

Private Function CheckValues( _
    ByRef sh As Worksheet, _
    ByVal ThisRow As Long, _
    ByVal ThisCol As Long, _
    ByRef MatchCell As Range)
Dim vecElements As Variant
Dim vecElements2 As Variant
Dim i As Long

    With sh.Cells(ThisRow, ThisCol)
    
        If .Value2 <> MatchCell.Offset(0, ThisCol - 1).Value2 Then
    
            vecElements = Split(.Value2, " ")
            vecElements2 = Split(MatchCell.Offset(0, ThisCol - 1).Value2, " ")
            For i = LBound(vecElements) To UBound(vecElements2)
            
                If vecElements(i) <> vecElements2(i) Then
                
                    .Characters(Application.Find(vecElements(i), .Value2), Len(vecElements(i))).Font.ColorIndex = 3
                End If
            Next i
        End If
    End With
End Function

This code unfortunately doesn't work without giving me syntax errors, however it does what I want when I'm able to bypass the error - it highlights ONLY the text portions within a given cell rather than highlighting the whole cell.

What I was wondering was... how would I combine these two codes into one? Meaning, instead of the red highlights for the whole cell, how do I make SHG's code do the text highlighting (as indicated by Bob's code above) rathen than the cell itself?

Thanks in advance!
JB
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top