VB Paste COUNTIF into range to find duplicates

gtvkeith

Board Regular
Joined
Mar 15, 2006
Messages
78
Hi everyone.

I am having some trouble with an error in this code.
I have a list of visitors names that grows everyday.
I want to use this list in a drop-down box, but eliminate any duplicate entries first.
This code will be in the Auto_Open macro and so recompile the list when the workbook is opened.

I have two problems
1. I get a type mismatch error on "If d.Value > 1 Then"
2. The formula in that is pasted in Column A has a "NAME?" error on the first part of the B:B range that I have made BOLD in the code.

Code:
'Paste COUNTIF formula
    Sheets("VisitorRef").Select
    LastRow = Cells(Rows.Count, "B").End(xlUp).Row
    Range("A1:A" & LastRow).FormulaR1C1 = "=COUNTIF([B]B[/B]:B,RC[+1])"
        
'Remove duplicate entries:
    For Each d In CountRange
        If d.Value > 1 Then
            d.EntireRow.Delete = True
        End If
    Next

I know it will probably be something simple but once again I am stumped and any help will be greatly appreciated.

Keith
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You need to delete rows backwards, starting from the bottom of the range and going upwards. You know the last row so use this instead:

Code:
dim r as long
For r = lastrow to 1 step -1

if .cells(r,1).value > 1 then rows(r).entirerow.delete

next r

That should solve it.
 
Upvote 0

Forum statistics

Threads
1,224,575
Messages
6,179,637
Members
452,934
Latest member
Jdsonne31

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