Case Sensitive removal of duplicates in VBA

awsumchillicrab

Board Regular
Joined
Jan 30, 2011
Messages
56
Hi all,

In removing duplicates with case-sensitivity, I can't use a COUNTIF so the substitute for counting is this formula entered with Ctrl-Shift-Enter:

=SUMPRODUCT(--EXACT($D$13:$D$16,D15))

Now I want to incorporate this into VBA, so that I can include the .delete method. How do I do it?

For starters, VBA doesn't seem to have WorksheetFunction.Exact
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
try this:

Code:
Option Compare Binary
Sub deleteExactDuplicates(ByVal rng As Range)
    Application.ScreenUpdating = False
    With CreateObject("scripting.dictionary")
        For Each i In rng.Cells
            v = i.Value
            If .exists(v) Then
                i.ClearContents
            Else
                .Add v, 1
            End If
        Next i
    End With
    On Error Resume Next
    rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
call the subroutine in your code by passing the range to be examined:
Code:
Sub test()
   deleteExactDuplicates Range("A1:A10")
End Sub
 
Upvote 0
Hi

Just a small remark:

An alternative to the

Code:
Option Compare Binary

that sets the comparison mode to the whole module, would be to just use in the dictionary object:

Code:
    With CreateObject("Scripting.Dictionary")
        .CompareMode = vbBinaryCompare
 
Upvote 0
Hi

Just a small remark:

An alternative to the

Code:
Option Compare Binary
that sets the comparison mode to the whole module, would be to just use in the dictionary object:

Code:
    With CreateObject("Scripting.Dictionary")
        .CompareMode = vbBinaryCompare

Nice - didn't know you could do that. Prefer it to the 'whole module' setting.
 
Upvote 0
try this:

Code:
Option Compare Binary
Sub deleteExactDuplicates(ByVal rng As Range)
    Application.ScreenUpdating = False
    With CreateObject("scripting.dictionary")
        For Each i In rng.Cells
            v = i.Value
            If .exists(v) Then
                i.ClearContents
            Else
                .Add v, 1
            End If
        Next i
    End With
    On Error Resume Next
    rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
call the subroutine in your code by passing the range to be examined:
Code:
Sub test()
   deleteExactDuplicates Range("A1:A10")
End Sub

for learning:

Can you please explain the code
 
Upvote 0
Upvote 0
Turns out the dictionary object is case sensitive by default. I'd not noticed this before.

Hi Weaver

Thank you. You are right. Maybe I should learn to think before I post. :(

I say the Option statement at the beginning of the module and since I usually use the .CompareMode property of the dictionary object I didn't even stop to think if this was needed here (not the first time it happens :)).

On the other hand, for the not-case-sensitive comparison, the .CompareMode property is handy:

http://www.mrexcel.com/forum/showthread.php?t=249483#7
 
Upvote 0
PGC - I wasn't suggesting your input was invalid, in fact, it was because of it that I did some research, in case there was anything else that I was missing. Turns out there wasn't, but worth a look anyway. There's a good link in another thread that Hech started on this subject.
 
Upvote 0
Yikes, this is some advanced stuff!

I'd have to take some time to figure this out.
I'm guessing there's no way to use the Excel formula

=SUMPRODUCT(--EXACT($D$13:$D$16,D15))

in VBA with .delete?
 
Upvote 0
Yikes, this is some advanced stuff!

I'd have to take some time to figure this out.
I'm guessing there's no way to use the Excel formula



in VBA with .delete?
There probably is, it's just that given the scope of your question, it's not the way I'd choose to go. I hope this solution works for you.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,839
Members
452,948
Latest member
UsmanAli786

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