删除Excel电子表格中的重复项

删除Excel电子表格中的重复项,excel,vba,Excel,Vba,我正在寻找某种宏,它可以删除电子表格中单元格中的重复单词 例如,如果单元格A1=“John John”,我希望我的宏删除重复的“John”。换句话说,A1将成为“约翰” 我发现了一组代码,我对其进行了一定程度的调整,以满足我的需要: Sub Remove_DupesInString() 'this loops through the specified range and erases duplicates Dim starval As String Dim finval As Strin

我正在寻找某种宏,它可以删除电子表格中单元格中的重复单词

例如,如果单元格A1=“John John”,我希望我的宏删除重复的“John”。换句话说,A1将成为“约翰”

我发现了一组代码,我对其进行了一定程度的调整,以满足我的需要:

Sub Remove_DupesInString()

'this loops through the specified range and erases duplicates


Dim starval As String
Dim finval As String
Dim strarray() As String
Dim x As Long
Dim y As Long
Dim k As Long
' step through each cell in range

For Each cell In Sheets(5).Range("D2:D6507")

            Erase strarray ' erase array
            finval = "" ' erase final value"


            starval = cell.Value

            strarray = Split(starval, " ") 'Seperator is space

            'Step through length of string and look for duplicate
            For rw = 0 To UBound(strarray)

                        For k = rw + 1 To UBound(strarray)
                                If Trim(strarray(k)) = Trim(strarray(rw)) Then
                                strarray(k) = ""  'if duplicate clear array value
                                End If
                         Next k
            Next rw
            ' combine all value in string less duplicate
            For x = 0 To UBound(strarray)
            If strarray(x) <> "" Then

            finval = finval & Trim(strarray(x)) & ", "
            End If

            Next x
             ' remove last space and comma
             finval = Trim(finval)
             finval = Left(finval, Len(finval) - 1)
             ' Replaces cells with new values
             cell.Value = finval

Next cell

End Sub
Sub-Remove_dupeinstring()
'这将在指定范围内循环并删除重复项
像绳子一样暗淡
作为字符串的Dim finval
Dim strarray()作为字符串
暗x等长
长得一样暗
暗k一样长
'逐步遍历范围内的每个单元格
表(5)中的每个单元格。范围(“D2:D6507”)
“擦除strarray”擦除数组
finval=“”删除最终值”
饥饿=单元格值
strarray=Split(饥饿,“”)分隔符是空格
'逐步检查字符串的长度并查找重复的字符串
对于rw=0至UBound(strarray)
k=rw+1至UBound(strarray)
如果纵倾(横倾(k))=纵倾(横倾(rw)),则
strarray(k)=“如果重复清除数组值”
如果结束
下一个k
下一个rw
'将所有值合并为无重复字符串
对于x=0至UBound(strarray)
如果strarray(x)“,则
finval=finval和Trim(strarray(x))和“,”
如果结束
下一个x
'删除最后一个空格和逗号
finval=修剪(finval)
finval=左(finval,Len(finval)-1)
'用新值替换单元格
cell.Value=finval
下一个细胞
端接头
这组代码对每个单元格中的空格都很敏感。如果在单元格D2中我有“John John”,在单元格D3中有“Mary”,则将生成以下内容:

D2=“约翰”,D3=“玛丽”

但是,如果我在运行宏的列中有空白单元格,它似乎不起作用。我已经解决了这个问题,对单元格进行排序,其中包含数据,并且只在这个范围内运行宏。
我已经尝试通过添加不同的
If
案例来进一步调整代码,其中包含
isEmpty()
然后但我在这里运气不好。我不太确定在
isEmpty
函数中放什么。有什么想法吗?

正如您所说,您唯一的问题似乎是处理空单元格。只要不处理单元格中不包含空格的任何单元格,就可以轻松解决这一问题:

Sub Remove_DupesInString()
    'this loops through the specified range and erases duplicates
    Dim starval As String
    Dim finval As String
    Dim strarray() As String
    Dim x As Long
    Dim y As Long
    Dim k As Long
    ' step through each cell in range

    For Each cell In Sheets(5).Range("D2:D6507")
        finval = "" ' erase final value"

        starval = cell.Value
        strarray = Split(starval, " ") 'Seperator is space
        If UBound(strarray) > LBound(strarray) Then 'i.e. there was a space
            'Step through length of string and look for duplicate
            For rw = 0 To UBound(strarray)
                For k = rw + 1 To UBound(strarray)
                    If Trim(strarray(k)) = Trim(strarray(rw)) Then
                        strarray(k) = ""  'if duplicate clear array value
                    End If
                Next k
            Next rw
            ' combine all value in string less duplicate
            For x = 0 To UBound(strarray)
                If strarray(x) <> "" Then
                    finval = finval & Trim(strarray(x)) & ", "
                End If
            Next x
            ' remove last space and comma
            finval = Trim(finval)
            finval = Left(finval, Len(finval) - 1)
            ' Replaces cells with new values
            cell.Value = finval
        End If    
    Next cell
End Sub
Sub-Remove_dupeinstring()
'这将在指定范围内循环并删除重复项
像绳子一样暗淡
作为字符串的Dim finval
Dim strarray()作为字符串
暗x等长
长得一样暗
暗k一样长
'逐步遍历范围内的每个单元格
表(5)中的每个单元格。范围(“D2:D6507”)
finval=“”删除最终值”
饥饿=单元格值
strarray=Split(饥饿,“”)分隔符是空格
如果UBound(strarray)>LBound(strarray),则“即存在一个空格
'逐步检查字符串的长度并查找重复的字符串
对于rw=0至UBound(strarray)
k=rw+1至UBound(strarray)
如果纵倾(横倾(k))=纵倾(横倾(rw)),则
strarray(k)=“如果重复清除数组值”
如果结束
下一个k
下一个rw
'将所有值合并为无重复字符串
对于x=0至UBound(strarray)
如果strarray(x)“,则
finval=finval和Trim(strarray(x))和“,”
如果结束
下一个x
'删除最后一个空格和逗号
finval=修剪(finval)
finval=左(finval,Len(finval)-1)
'用新值替换单元格
cell.Value=finval
如果结束
下一个细胞
端接头

欢迎来到堆栈溢出。这不是一个code/SQL/regex编写服务,在这里,您发布了一个需求列表和选择的语言,然后一个代码猴子为您编写代码。我们非常乐意提供帮助,但我们希望您首先自己努力解决问题。一旦你这样做了,你可以解释你的问题,包括你工作的相关部分,并提出一个具体的问题,我们会尽力帮助你。祝你好运。如果A3单元包含“Leele”,A4单元包含“Lee”,A3单元是否应该改为“Lee”?i、 e.案例和/或嵌入空间是否重要?如果A5单元包含“Lee Anne”,那么它应该保持不变,还是改为“Anne”,或者改为“Lee”?从代码判断,A2(和D3)包含“John”的事实与问题无关——您只是试图删除每个单元中的任何重复单词。因此,如果A1有“John John”,A2有“Mary”(即使“John”不存在于电子表格中的其他单元格中),您仍然希望A1成为“John”。对吗?对,对。我只想删除每个单元格中重复的单词。这就像一个符咒!我向你致敬,先生!非常感谢。