Problem with Code

Trevasaurus

New Member
Joined
Dec 4, 2012
Messages
13
I have this code for comparing two columns, finding repeated entries, and deleting the row where the entry is repeated. I've tested it on a small data base (<50 entries) and it works. However, when I apply it to a larger database (>300), I'm getting an error: "Subscript Out of Range"

Sub find_copy01()
Dim c2 As Range, c As Range
With Sheets("Sheet2")
LR = .Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For Each c2 In .Range("A1:A" & LR)
Set c = Sheets("Sheet1").Columns(1).Find(c2, , xlValues, xlWhole)
If Not c Is Nothing Then
c.EntireRow.Interior.Color = vbRed
c2.EntireRow.Delete
End If
Next c2
End With
End Sub

Can anyone tell me what is wrong?
 
Trev,

The code worked for me in your workbook once I changed "Sheet1" to match your actual sheet name.

I didn't see my suggested macro in your file, so I don't know if you had been trying it with "Sheet1" or the actual sheet name.
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Trev,

Thanks for providing feedback to confirm the cause. I'm glad it was something easy to fix. :)

Referencing worksheets can be confusing when first working with VBA because there are several similar ways to do the same thing.

Some examples:

Code:
[B]Sheets("Sheet1").Select[/B]     ' This references the sheet that reads "Sheet1" (no quotes) on the tab - 
                                    regardless of what order it falls within all tabs.

[B]Sheets(1).Select  [/B]          ' This references the first sheet in the workbook - 
                                     regardless of its sheet name.

[B]Sheet1.Select[/B]               ' This references the CodeName for the Sheet-
                                     regardless of sheetname or order.
                                     CodeNames can be seen in the Properties Window of the VB Editor.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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