VBA Interior ColorIndex not working on a match

storm925

Board Regular
Joined
Jan 20, 2010
Messages
226
Hi All,

Below is my code that I can't get to work...
Code:
Sub find_dup_pairs()


Dim i As Long
Dim LastRow As Long


LastRow = Range("B" & Rows.Count).End(xlUp).Row


For i = LastRow To 1 Step -1
    If Cells(i, 2) & Cells(i, 3) = Cells(i + 1, 2) & Cells(i + 1, 3) Then
        'Range(Cells(i, 2), Cells(i, 3)).Select   'this works...
        Range(Cells(i, 2), Cells(i, 3)).Interior.ColorIndex = 255   'this does not work...
    End If
Next i
End Sub

The code works if I want to "Select" cells B and C if there is a duplicate. When I turn off the duplicate, and just want to highlight the duplicate pairs, an error message is thrown that says "subscript out of range". I would like to highlight pairs without having to select each one if that is possible. Thanks for reading my post and your help on this. --Ben
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Thank you for the response. I changed the number to 6 (for yellow), but I still get the same error.
 
Upvote 0
Your code works for me.
Do you have any merged cells?
 
Upvote 0
I'm really embarrassed to say this, but my sheet had restrictions on it. Once I removed the restrictions, it worked. Thanks again for looking at my code and I'm sorry to waste your time... feeling quite dumb right now.
 
Upvote 0
No worries
Glad to help & thanks for the feedback
 
Upvote 0
For a quick reference to see all the Colorindex numbers run this small script:

Code:
Sub Color_Index_Me()
Dim i As Long
Dim c As Long
c = ActiveCell.Column
    For i = 1 To 56
        With Cells(i, c)
            .Value = i
            .Interior.ColorIndex = i
            .Font.Size = 18
        End With
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

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