在Excel VBA中使用LIKE查找单元格的重复项?

我正在清理一些可怕的数据。 我有一个列描述,包含各个部分的描述。 谁维护这个数据似乎没有任何方法来疯狂,但这是另一次。

我试图find重复的数据并将其标记为重复项。 不幸的是,由于这件事情是如何做的,这可能是艰难的。 例如,让我们来描述“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 

有没有办法在代码运行时捕获字典条目的所有可能的变体,类似于你如何在SQL中使用LIKE?

听起来你想在Excel中做正则expression式通配符匹配。 您可以尝试查看内置的Range.Find,看看是否可以完成您所需要的。 我发现这个关于在VBA中匹配string的答案,然后引导我去使用.Find 这个post。

看看你是否可以使用下面的例子来构build。

 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