简体   繁体   中英

compare two columns in two different sheet then highlight the differences

I have two sheets in the same workbook. In each one, I have a table. I want to write a macro that compare the 2 tables then highlight lines if there are more lines in the second sheet. I wrote some lines but I don't know how to continue the loop.

 lr1 = Worksheets("Analyse de risque").Range("B" & Worksheets("Analyse de risque").Rows.Count).End(xlUp).Row
  lr2 = Worksheets("Plan Traitement Risque").Range("B" & Worksheets("Plan Traitement Risque").Rows.Count).End(xlUp).Row
  With ThisWorkbook.Sheets("Plan Traitement Risque")
    For i = 6 To lr1 Step 1
    foundTrue = False
        For j = 6 To lr2 Step 1
        If Sheets("Analyse de risque").Cells(i, 2).Value = Sheets("Plan Traitement Risque").Cells(j, 2).Value Then
        foundTrue = True
        Exit For
    End If

Next j

Try this. It should mark values that are present only in one of the sheets - now it uses the strikethrough to mark the lines. But just changing what the macro does to a cell should be quite easy for you. Start recording and set strikethrough on a some text and you should have it in no time.

    Sub highlight_differences()

lr1 = Worksheets("Analyse de risque").Range("B" & Worksheets("Analyse de risque").Rows.Count).End(xlUp).Row
lr2 = Worksheets("Plan Traitement Risque").Range("B" & Worksheets("Plan Traitement Risque").Rows.Count).End(xlUp).Row

    Worksheets("Plan Traitement Risque").Range("B6:B" & lr2).Font.Strikethrough = True


    For i = 6 To lr1 Step 1
    foundTrue = False
        For j = 6 To lr2 Step 1

            If Sheets("Analyse de risque").Cells(i, 2).Value = Sheets("Plan Traitement Risque").Cells(j, 2).Value Then
                foundTrue = True
                Worksheets("Plan Traitement Risque").Range("B" & j).Font.Strikethrough = False

                Exit For
            End If

        Next j

        If foundTrue = False Then
            Worksheets("Analyse de risque").Range("B" & i).Font.Strikethrough = True

        End If

    Next i

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM