Finding Duplicates

Maccers93

New Member
Joined
Feb 12, 2021
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Hey guys,

Just wondering, I keep getting error 91 when running the below:
VBA Code:
Sub HighlightCellIfValueExistsinAnotherColumn()
    Dim ws As Worksheet
    Dim i As Long
    Dim j As Long
    Dim LastRow As Long
    Dim LastRow2 As Long

LastRow = Cells(Rows.Count, "Q").End(xlUp).Row
LastRow2 = Cells(Rows.Count, "A").End(xlUp).Row

For j = 2 To LastRow2
    For i = 2 To LastRow
    If ws.Range("A" & i).Value <> ws.Range("Q" & j).Value Then GoTo NextInteration
        If ws.Range("A" & i).Value = ws.Range("Q" & j).Value Then
            ws.Range("A" & i).Interior.ColorIndex = 4 'green
            ws.Range("Q" & j).Interior.ColorIndex = 4 'green
            GoTo NextInteration 'otherwise next interation
        End If
NextInteration:
    Next i
Next j
End Sub

Anyone lend a hand please I would greatly appreciate it?

Thanks in advance!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Whilst you have declared ws as worksheet, you haven't actually set it to anything.
 
Upvote 0
How about
VBA Code:
Sub HighlightCellIfValueExistsinAnotherColumn()
    Dim ws As Worksheet
    Dim i As Long
    Dim j As Long
    Dim LastRow As Long
    Dim LastRow2 As Long

Set ws = Sheets("Sheet1")
LastRow = ws.Cells(Rows.Count, "Q").End(xlUp).Row
LastRow2 = ws.Cells(Rows.Count, "A").End(xlUp).Row

For j = 2 To LastRow2
    For i = 2 To LastRow
    If ws.Range("A" & i).Value <> ws.Range("Q" & j).Value Then GoTo NextInteration
        If ws.Range("A" & i).Value = ws.Range("Q" & j).Value Then
            ws.Range("A" & i).Interior.ColorIndex = 4 'green
            ws.Range("Q" & j).Interior.ColorIndex = 4 'green
            GoTo NextInteration 'otherwise next interation
        End If
NextInteration:
    Next i
Next j
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub HighlightCellIfValueExistsinAnotherColumn()
    Dim ws As Worksheet
    Dim i As Long
    Dim j As Long
    Dim LastRow As Long
    Dim LastRow2 As Long

Set ws = Sheets("Sheet1")
LastRow = ws.Cells(Rows.Count, "Q").End(xlUp).Row
LastRow2 = ws.Cells(Rows.Count, "A").End(xlUp).Row

For j = 2 To LastRow2
    For i = 2 To LastRow
    If ws.Range("A" & i).Value <> ws.Range("Q" & j).Value Then GoTo NextInteration
        If ws.Range("A" & i).Value = ws.Range("Q" & j).Value Then
            ws.Range("A" & i).Interior.ColorIndex = 4 'green
            ws.Range("Q" & j).Interior.ColorIndex = 4 'green
            GoTo NextInteration 'otherwise next interation
        End If
NextInteration:
    Next i
Next j
End Sub
The simplest things would drive me mad, thank you for your assistance!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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