Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2009|回复: 9

[已解决]求助:用V代码如何提取不重复的姓名

[复制链接]
发表于 2010-10-22 20:03 | 显示全部楼层 |阅读模式

如题,如何写提取不重复姓名的代码?要求在附件中,请老师同学们指点.谢谢

03rdfMF1.rar (1.71 KB, 下载次数: 17)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2010-10-22 20:29 | 显示全部楼层

如果数据不多,就不需要用数组和字典了!!请测试: eYC1EmR0.rar (9.21 KB, 下载次数: 6)
回复

使用道具 举报

发表于 2010-10-22 20:29 | 显示全部楼层    本楼为最佳答案   

  1. Sub justtest()<br/>&nbsp;&nbsp;&nbsp; Dim dic, arr, i&amp;, j&amp;<br/>&nbsp;&nbsp;&nbsp; Set dic = CreateObject("scripting.dictionary")<br/>&nbsp;&nbsp;&nbsp; arr = Range("A2").CurrentRegion.Value<br/>&nbsp;&nbsp;&nbsp; For i = 2 To UBound(arr, 1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For j = 1 To UBound(arr, 2) - 1 Step 2<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr(i, j) &lt;&gt; "" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If dic.exists(arr(i, j)) Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dic(arr(i, j)) = dic(arr(i, j)) + arr(i, j + 1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else: dic.Add arr(i, j), arr(i, j + 1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Next j, i<br/>&nbsp;&nbsp;&nbsp; Range("f2:g" &amp; Rows.Count).ClearContents<br/>&nbsp;&nbsp;&nbsp; If dic.Count &gt; 0 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cells(2, "f").Resize(dic.Count, 1) = Application.Transpose(dic.keys)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cells(2, "g").Resize(dic.Count, 1) = Application.Transpose(dic.items)<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Set dic = Nothing<br/>End Sub
复制代码

字典的强项.

f7OH02hS.rar (9.28 KB, 下载次数: 71)
回复

使用道具 举报

 楼主| 发表于 2010-10-22 20:38 | 显示全部楼层

感谢两位老师,都可以实现,但3楼的速度快了很多,感谢了.还不是很理解,学习中
回复

使用道具 举报

发表于 2010-10-22 20:48 | 显示全部楼层

3楼代码 有部分不必要的代码

回复

使用道具 举报

发表于 2010-10-22 20:59 | 显示全部楼层

Sub aa()
    Dim d As Object
    Dim arr
    Dim i As Long
    Dim j As Long
    Set d = CreateObject("scripting.dictionary")
    arr = Range("A2").CurrentRegion.Value
    For i = 2 To UBound(arr, 1)
        For j = 1 To 4 Step 2
            If arr(i, j) <> "" Then
                d(arr(i, j)) = d(arr(i, j)) + arr(i, j + 1)
            End If
        Next j
    Next i
    Range("F2").Resize(d.Count, 1) = Application.Transpose(d.keys)
    Range("G2").Resize(d.Count, 1) = Application.Transpose(d.items)
End Sub
字典遇到没有的会自动创建

回复

使用道具 举报

发表于 2010-10-22 21:22 | 显示全部楼层

就是,并且还要快些
回复

使用道具 举报

 楼主| 发表于 2010-10-22 23:17 | 显示全部楼层

QUOTE:
以下是引用搁浅2008在2010-10-22 20:59:00的发言:

Sub aa()
    Dim d As Object
    Dim arr
    Dim i As Long
    Dim j As Long
    Set d = CreateObject("scripting.dictionary")
    arr = Range("A2").CurrentRegion.Value
    For i = 2 To UBound(arr, 1)
        For j = 1 To 4 Step 2
            If arr(i, j) <> "" Then
                d(arr(i, j)) = d(arr(i, j)) + arr(i, j + 1)
            End If
        Next j
    Next i
    Range("F2").Resize(d.Count, 1) = Application.Transpose(d.keys)
    Range("G2").Resize(d.Count, 1) = Application.Transpose(d.items)
End Sub
字典遇到没有的会自动创建

学习,感谢老师

回复

使用道具 举报

发表于 2010-10-24 06:15 | 显示全部楼层

学习字典开始就要养成好的习惯。

比如用dic.exists判断存在与否

比如判断DIC.COUNT是否>0。。。。

回复

使用道具 举报

发表于 2013-4-12 15:32 | 显示全部楼层
好好学习天天向上
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-14 08:03 , Processed in 0.329080 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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