Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
123
返回列表 发新帖

[已解决]关于字典的问题

[复制链接]
 楼主| 发表于 2014-9-13 21:54 | 显示全部楼层
su45 发表于 2014-9-13 18:24
你没认真试吧?

看附件:

如果在后面单元格填上别的代码,前面的供应商本来为空的,结果是这样。1234前面还有供应商。
2.png
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2014-9-13 23:24 | 显示全部楼层
你的附件中,那列本应是空的!你使用宏之前是不是应先清空呀!
回复

使用道具 举报

发表于 2014-9-13 23:27 | 显示全部楼层
  1. Sub 基础表整理()
  2. On Error Resume Next
  3.     Dim a, c As Range, Rng As Range, RG As Range
  4.     Dim Hx As Long, arr, Brr, d As Object, i&
  5.     With Sheets("产品信息")
  6.         arr = .Range("a2:b" & .Range("a65536").End(xlUp).Row)
  7.     End With
  8.     Set d = CreateObject("scripting.dictionary")
  9.     For i = 1 To UBound(arr)
  10.         If Not d.exists(arr(i, 1)) Then
  11.             d(arr(i, 1)) = arr(i, 2)
  12.         Else
  13.             d(arr(i, 1)) = d(arr(i, 1)) & "," & arr(i, 2)
  14.         End If
  15.     Next i
  16.     Brr = Sheets("基础表").Range("d2:d" & Sheets("基础表").Range("d65536").End(xlUp).Row)
  17.     With Columns("C:C")
  18.         .Validation.Delete
  19.         .Font.ColorIndex = 1
  20.     End With
  21.     For i = 1 To UBound(Brr)
  22.         If d.exists(Brr(i, 1)) Then
  23.             If InStr(d(Brr(i, 1)), ",") Then
  24.                 With Cells(i + 1, 3).Validation
  25.                     .Add 3, 1, 1, d(Brr(i, 1))
  26.                 End With
  27.                 Cells(i + 1, 3) = Split(d(Brr(i, 1)), ",")(0)
  28.                 Cells(i + 1, 3).Font.ColorIndex = 3
  29.             Else
  30.                 Cells(i + 1, 3) = d(Brr(i, 1))
  31.             End If
  32.         Else
  33.             Cells(i + 1, 3) = ""
  34.         End If
  35.     Next
  36. End Sub
复制代码

评分

参与人数 1 +1 收起 理由
如履薄冰_﹌ + 1 赞一个!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2014-9-13 23:35 | 显示全部楼层
su45 发表于 2014-9-13 23:27

这样就对了,给你10个赞。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-30 00:45 , Processed in 0.305818 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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