ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 3868|回复: 12

[求助] 请教字符串去重复

[复制链接]

TA的精华主题

TA的得分主题

发表于 2017-9-10 10:10 | 显示全部楼层 |阅读模式
请教:表中A列是有规律用逗号相隔的字符,想去重复达到B列的效果。
本人试写了代码,但去重出现累加问题,怎么处理?谢谢!

字符串去重复.rar (10.09 KB, 下载次数: 47)

TA的精华主题

TA的得分主题

发表于 2017-9-10 10:24 | 显示全部楼层
Sub 字符去重复2()
Dim d, arr, i&, j&, n&
Set d = CreateObject("Scripting.Dictionary")
arr = [A2:A10]
For i = 1 To UBound(arr)
  p = Split(arr(i, 1), ",")
  For j = 0 To UBound(p)
    If Not d.exists(p(j)) Then
      s = s & "," & p(j)
      d(p(j)) = ""
    End If
  Next
  arr(i, 1) = Mid(s, 2)
  s = ""
  d.RemoveAll
Next
[d2].Resize(UBound(arr), 1) = arr
Set d = Nothing
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2017-9-10 10:25 | 显示全部楼层
  1. Sub 字符去重复ByMicro()
  2.     Dim vData As Variant, nRow As Integer
  3.     Dim dicData As Object, vDic As Variant, nDic As Integer
  4.    
  5.     Application.ScreenUpdating = False
  6.     Set dicData = CreateObject("Scripting.Dictionary")
  7.     vData = Sheet4.UsedRange.Offset(1).Resize(, 1).Value
  8.     For nRow = 1 To UBound(vData) - 1
  9.         vDic = Split(vData(nRow, 1), ",")
  10.         dicData.RemoveAll
  11.         For nDic = 0 To UBound(vDic)
  12.             dicData(vDic(nDic)) = 0
  13.         Next
  14.         vData(nRow, 1) = ""
  15.         For Each vDic In dicData
  16.             If vData(nRow, 1) <> "" Then vData(nRow, 1) = vData(nRow, 1) & ","
  17.             vData(nRow, 1) = vData(nRow, 1) & vDic
  18.         Next
  19.     Next
  20.     Sheet4.[C2].Resize(UBound(vData) - 1) = vData
  21. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2017-9-10 11:41 | 显示全部楼层
不需词典
  1. Sub 字符去重复()
  2.     Dim br, i, j, brr(), s, p
  3.     Sheet4.Range("C1:C10000").ClearContents
  4.     br = Sheet4.Range("A2:A10")
  5.     For i = 1 To UBound(br)
  6.         p = Split(br(i, 1), ",")
  7.         s = ""
  8.         For j = 0 To UBound(p)
  9.             If InStr(s, p(j)) = 0 Then
  10.                 s = s & "," & p(j)
  11.             End If
  12.         Next
  13.         br(i, 1) = Right(s, Len(s) - 1)
  14.     Next
  15.     Sheet4.[C2].Resize(UBound(br), 1) = br
  16. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2017-9-10 12:07 | 显示全部楼层
Sub 字符去重复()
    Set d = CreateObject("Scripting.Dictionary")
    arr = [a1].CurrentRegion
    For i = 1 To UBound(arr)
        s = Split(arr(i, 1), ",")
        For j = 0 To UBound(s)
            d(s(j)) = ""
        Next
        arr(i, 1) = Join(d.keys(), ",")
        d.RemoveAll
    Next
    arr(1, 1) = "想要的结果【去重复保留唯一的】"
    [a1].CurrentRegion.Offset(, 1).ClearContents
    [b1].Resize(UBound(arr), 1) = arr
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2017-9-10 12:23 | 显示全部楼层

如果是 BAA1,AA1,A1,A1

後面的AA1,A1都會被吃掉~~

TA的精华主题

TA的得分主题

发表于 2017-9-10 22:10 | 显示全部楼层
Sub 字符去重複_AmoKat()
    Dim xR As Range, S$, i%
    Set xR = Cells(2, 1)
    Do While xR <> ""
        Arr = Split(xR, ",")
        For i = 0 To UBound(Arr)
            If Application.Match(Arr(i), Arr, 0) = i + 1 Then S = S & IIf(S = "", "", ",") & Arr(i)
        Next
        xR(1, 3) = S: S = ""
        Set xR = xR(2, 1)
    Loop
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2017-9-11 10:10 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
另類法:
Sub test1()
Dim xR As Range, AA, T$, i%
For Each xR In Range([A2], [A65536].End(xlUp))
  AA = Split(xR, ",")
  For i = UBound(AA) To 0 Step -1
    T = AA(i) & " " & Trim(Replace(" " & T & " ", " " & AA(i) & " ", " "))
  Next i
  xR(1, 3) = Replace(Trim(T), " ", ","): T = ""
Next
End Sub
 
 

TA的精华主题

TA的得分主题

发表于 2017-9-11 10:11 | 显示全部楼层
也可以用自訂函數來處理
Function StrRmvDmp(Str)
    S = Split(Str, ",")
    Set d = CreateObject("Scripting.Dictionary")
    For i = 0 To UBound(S): d(S(i)) = i: Next
    StrRmvDmp = Join(d.keys(), ",")
End Function

Sub ex1()
    With [F2].Resize(Application.CountA([A:A]))
        .Value = "=StrRmvDmp(A2)"
        .Value = .Value
    End With
End Sub

TA的精华主题

TA的得分主题

发表于 2017-9-11 10:15 | 显示全部楼层
INSTR法:
Sub test2()
Dim xR As Range, AA, T$
For Each xR In Range([A2], [A65536].End(xlUp))
  For Each AA In Split(xR, ",")
    If InStr("," & T & ",", "," & AA & ",") = 0 Then T = T & "," & AA
  Next
  xR(1, 3) = Mid(T, 2): T = ""
Next
End Sub
 
 
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-17 05:25 , Processed in 0.037570 second(s), 18 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表