Home » excel » Using LIKE in Excel VBA to find duplicates of a cell?

Using LIKE in Excel VBA to find duplicates of a cell?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to clean up some horrendous data. I have a column, DESCRIPTION, that contains descriptions of various parts. Whoever maintained this data didn’t seem to have any method to his or her madness, but that’s for another time.

I’m trying to find duplicates of data and mark them as duplicates. Unfortunately, because of how this thing was made, it can be tough. For example, let’s take the description “BEARING”. Using my current code, I can only catch cells that say “BEARING”. If they say “BEARING,” or “BEARING ” (note the space) or “BEARING, ” (again, a space) then it won’t catch it:

Sub test()
    Dim uniqueCounter As New Scripting.Dictionary
    Dim counter As Long
    Dim rowCount As Long
    Dim identifier As String


    rowCount = 10235

    uniqueCounter.CompareMode = TextCompare

    For counter = 1 To rowCount
        identifier = ActiveSheet.Cells(counter, 3) 'Put whatever number of combination of cells which the row unique here (maybe it's just the one)
        If uniqueCounter.Exists(identifier) Then
            uniqueCounter(identifier) = CLng(uniqueCounter(CStr(ActiveSheet.Cells(counter, 3)))) + 1
            ActiveSheet.Cells(counter, 1) = "Duplicate " & identifier
        Else
            uniqueCounter.Add identifier, "0"
            ActiveSheet.Cells(counter, 1) = "Original " & identifier
        End If
    Next counter

End Sub

Is there a way to catch all the possible variants of a dictionary entry when the code runs, similar to how you can use LIKE in SQL?

How to&Answers:

Sounds like you’re wanting to do regex wildcard matching in Excel. You might try looking at the built-in Range.Find and see if that can accomplish what you need. I found this answer about matching strings in VBA, which then led me to this post about using .Find.

Answer:

See if you can build using the example below.

Option Explicit
Option Compare Text

Sub test()
'the following code does a case-insensitive and wildcard comparison,returns true 
Debug.Print "bearing" Like "Bearing*"   
Debug.Print "bearing " Like "Bearing*"
Debug.Print "bearing, " Like "Bearing*"

End Sub