excel - 删除重复行,仅针对列中的每个单元格

标签 excel vba

enter image description here

我在每个单元格上都有重复的行,这些行只是用 vbLf 分隔的 URL 地址。
我需要删除重复行,但只删除列中的每个单元格
我找到了下面的函数,但它只删除每个单元格的单词。
预先感谢任何有用的评论和答案。

Function RemoveDupeWords(text As String, Optional delimiter As String = " ") As String
    Dim dictionary As Object
    Dim x, part
 
    Set dictionary = CreateObject("Scripting.Dictionary")
    dictionary.CompareMode = vbTextCompare
    For Each x In Split(text, delimiter)
        part = Trim(x)
        If part <> "" And Not dictionary.Exists(part) Then
            dictionary.Add part, Nothing
        End If
    Next
 
    If dictionary.Count > 0 Then
        RemoveDupeWords = Join(dictionary.keys, delimiter)
    Else
        RemoveDupeWords = ""
    End If
 
    Set dictionary = Nothing
End Function

最佳答案

也许这会成功

    sub RemoveDupeLine() 
  
    Dim coll As Collection
    
    Dim i As Long, c, txt As String, arr As Variant
    
    Dim rng As Range: Set rng = ThisWorkbook.Worksheets(1).Range("A1").CurrentRegion
    
    For Each c In rng
        Set coll = New Collection
        arr = Split(c.Value, vbLf)
          For i = LBound(arr) To UBound(arr)
    ' Here's the trick. For a collection in this insert mode it cannot accept duplicate key entries. And in that I instruct him to ignore the error, he accumulates only unique values and move on.
                On Error Resume Next
                  coll.Add Item:=arr(i), Key:=arr(i)
                On Error GoTo 0
          Next i
          
       If coll.Count > 0 Then
            For i = 1 To coll.Count
               txt = txt & coll(i) & vbLf
            Next i
       End If
         c.Value = txt
         txt = ""
        Set coll = Nothing
        
    Next
     
End sub

祝你好运

关于excel - 删除重复行,仅针对列中的每个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72150777/

相关文章:

python - 如何将本 map 片的超链接写入openpyxl中的单元格?

string - MATLAB 字符串变量

excel - 从工作表名称创建图表标题

vba - 带有文本框字符串比较的多个 IF 语句

VBA通过文本框实时过滤列表框

c# - 使用 .NET Core 在 Mac OSX 上进行 Office (Excel) COM 互操作?

excel - 有没有办法使用通配符来计算日期?

excel - 有没有办法将 Excel 的最大行限制设置为 1,048,576

vba - 你会推荐什么教程来学习 PowerPoint VBA?

excel - VBA - 将循环内的地址存储为新的范围变量