+ -
当前位置:首页 → 问答吧 → 一个字典的问题,请高人指点下? 谢谢

一个字典的问题,请高人指点下? 谢谢

时间:2011-08-11

来源:互联网

代码的作用是提取不重复数据 并统计重复了几次 之后返回到指定单元格。不过 有些不理解,


17 秒前
Sub test2()
  Dim dic As Object, i%
    Set dic = CreateObject("scripting.dictionary")
    For i = 2 To Cells(Rows.Count, 1).End(3).Row
      If dic.exists(Cells(i, 1).Value) Then dic(Cells(i, 1).Value) = dic(Cells(i, 1).Value) + 1 Else: dic(Cells(i, 1).Value) = 1
   
    '               Cells(i, 1).Value是对KEY判断么,但是后面指句dic(Cells(i, 1).Value) = 1又感觉Cells(i, 1).Value表示的是VALUE
    '  不理解???
    Next i
    Range("e:G").Clear
    Cells(1, 5).Resize(1, 2) = Array("编号", "个数")
    Cells(2, 5).Resize(dic.Count, 1) = Application.Transpose(dic.keys)

    '上面的代码好像都没有对KEY赋值(只是对ITEM.VALUE赋值),那么哪里来的KEYS 不理解?    Cells(2, 6).Resize(dic.Count, 1) = Application.Transpose(dic.items)
    Set dic = Nothing
End Sub

附件

que.rar(12.37 KB)

2011-8-11 19:30, 下载次数: 2

作者: andrewyang   发布时间: 2011-08-11

这个看蓝版那个字典的帖子就好了吧,有详解。。。

作者: jiminyanyan   发布时间: 2011-08-11

做个记号

作者: 学以用之   发布时间: 2011-08-11

If dic.exists(Cells(i, 1).Value) Then dic(Cells(i, 1).Value) = dic(Cells(i, 1).Value) + 1 Else: dic(Cells(i, 1).Value) = 1
如果存在Cells(i, 1).Value这个KEY,则item累加,否则赋初始值
dic(Cells(i, 1).Value) = 1 这句赋KEY为Cells(i, 1).Value,ITEM为1

作者: doitbest   发布时间: 2011-08-11

If dic.exists(Cells(i, 1).Value) Then
dic(Cells(i, 1).Value) = dic(Cells(i, 1).Value) + 1
Else
dic(Cells(i, 1).Value) = 1
这个应该知道

作者: wj2368   发布时间: 2011-08-11

热门下载

更多