|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 zhaogang1960 于 2013-11-25 15:47 编辑
提高速度改进:
一、昨天用了两个字典,都使用了字典嵌套,菜单有些迟缓,今天试着去掉第二个字典,用字典条目Items来代替第二个字典,以达到加快速度的目的
二、加进了各位的改进- Sub CreatMe() '生成右键菜单
- Dim d As Object, i&, j&, k, k2, t, a, l&, arr, x As Object
- Set d = CreateObject("scripting.dictionary")
- arr = Sheets("Sheet1").Range("A1").CurrentRegion
- For i = 2 To UBound(arr)
- If Not d.Exists(arr(i, 1)) Then Set d(arr(i, 1)) = CreateObject("scripting.dictionary")
- If Len(arr(i, 2)) Then d(arr(i, 1))(arr(i, 2)) = d(arr(i, 1))(arr(i, 2)) & "," & arr(i, 3)
- Next
- k = d.keys '一级分类
- With Application.CommandBars("cell")
- For Each x In .Controls '删除所有菜单项
- x.Delete
- Next
- For i = 0 To UBound(k)
- With .Controls.Add(Type:=IIf(d(k(i)).Count, msoControlPopup, msoControlButton))
- .Caption = k(i)
- .OnAction = IIf(d(k(i)).Count, "", "'显示在活动单元格 """ & k(i) & """'")
- .BeginGroup = True '分组显示
- k2 = d(k(i)).keys '二级分类
- t = d(k(i)).items '三级分类,每个三级分类用逗号隔开
- For j = 0 To UBound(k2)
- a = Split(t(j), ",")
- With .Controls.Add(Type:=IIf(Len(t(j)) > UBound(a), msoControlPopup, msoControlButton))
- .Caption = k2(j)
- .OnAction = IIf(Len(t(j)) > UBound(a), "", "'显示在活动单元格 """ & k2(j) & """'")
- For l = 1 To UBound(a)
- If Len(a(l)) Then
- With .Controls.Add(Type:=msoControlButton)
- .Caption = a(l)
- .OnAction = "'显示在活动单元格 """ & a(l) & """'"
- End With
- End If
- Next
- End With
- Next
- End With
- Next
- End With
- End Sub
复制代码 |
|