Home » excel » Highlighting duplicate "Q&A" in excel but not first

Highlighting duplicate "Q&A" in excel but not first

Posted by: admin May 14, 2020 Leave a comment

Questions:
What is the color of the sky?
A. Blue
B. Grey
C. Green
A

Which is the Capital of Japan?
A. Alabama
B. Alaska
C. Tokyo
B

What is the letter next to C
A. D
B. E
C. F
A

Which is the Capital of Japan?
A. Alabama
B. Alaska
C. Tokyo
B

i have about 1000 Questions, and i would like to remove the duplicates OR highlight the duplicate ones except for the first, so i could delete them.

The Question is in one row, Answer is in the next row.
Sample here

Please help me.

Thanks 🙂

How to&Answers:

NON VBA solution for highlighting duplicates:

You can use Conditiona Formatting.

Suppose your Q/A in range A2:A12.

Step 1:

Select range A2:A12. With selected range go to Conditional Formatting –> New Rule…

enter image description here

Step 2:

Select “Use formula to detect which cells to format”, enter formula =AND(LEN(A2)>1,ISNUMBER(MATCH(A2,$A$1:$A1,0))) and choose desired format. Click OK.

enter image description here

Result:

enter image description here

Notes:

  1. If your Q/A list starts from row 1, apply CF only for questions starts from row 2 (formula requirements). Шt will not affect the result because in the first row you can’t have duplicate.
  2. In formula =AND(LEN(A2)>1,ISNUMBER(MATCH(A2,$A$1:$A1,0))) part LEN(A2)>1 exclude answers from duplicate search (because they could be duplicate for different answers)

VBA soluton for highlighting/deleting duplicates:

Sub test()
    Dim lastrow As Long
    Dim rngToDel As Range

    'change Sheet1 to suit
    With ThisWorkbook.Worksheets("Sheet1")
        'change column A to column where your Q/A list
        lastrow = .Cells(Rows.Count, "A").End(xlUp).Row

        'loop throught cells from lastrow to row ¹2
        For i = lastrow To 2 Step -1
            'if it's question and not answer
            If Len(.Range("A" & i)) > 1 Then
                'if it is a duplicate
                If Not IsError(Application.Match(.Range("A" & i), .Range("A1:A" & i - 1), 0)) Then
                    If rngToDel Is Nothing Then
                        'it's a duplicate - remember both question and answer
                        Set rngToDel = Union(.Range("A" & i), .Range("A" & i + 1))
                    Else
                        Set rngToDel = Union(rngToDel, .Range("A" & i), .Range("A" & i + 1))
                    End If
                End If
            End If
        Next i
    End With

    If Not rngToDel Is Nothing Then
        'highlight entire row with red
        rngToDel.EntireRow.Interior.Color = RGB(255, 0, 0)
        'or delete entire row
        rngToDel.EntireRow.Delete
    End If
End Sub