简体   繁体   English

在Excel VBA中使用LIKE查找单元格的副本?

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

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". 例如,让我们来描述“BEARING”。 Using my current code, I can only catch cells that say "BEARING". 使用我当前的代码,我只能捕获说“BEARING”的单元格。 If they say "BEARING," or "BEARING " (note the space) or "BEARING, " (again, a space) then it won't catch it: 如果他们说“BEARING”或“BEARING”(注意空间)或“BEARING”(再次,一个空格),那么它将无法捕获它:

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? 有没有办法在代码运行时捕获字典条目的所有可能变体,类似于如何在SQL中使用LIKE?

Sounds like you're wanting to do regex wildcard matching in Excel. 听起来你想要在Excel中进行正则表达式通配符匹配。 You might try looking at the built-in Range.Find and see if that can accomplish what you need. 您可以尝试查看内置的Range.Find,看看是否可以实现您的需求。 I found this answer about matching strings in VBA, which then led me to this post about using .Find. 我发现这个答案有关VBA字符串匹配,然后领我到这个职位有关使用.Find。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM