|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
笑着...两年过去 发表于 2013-11-25 17:23
额,我不懂所以叫法不对,反正差不多右键的那个效果,您懂的
激活Sheet2时生成左键树型菜单,Sheet2出于非活动状态时删除它,当你单击F列的某一个单独的单元格时显示左键树型菜单,离开时不显示,速度很快:- 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 '一级分类
- On Error Resume Next
- Application.CommandBars("树型菜单").Delete '删除可能存在的
- With Application.CommandBars.Add("树型菜单", msoBarPopup)
- 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
复制代码- Sub DeleteMycell() '删除左键菜单
- On Error Resume Next
- Application.CommandBars("树型菜单").Delete
- End Sub
复制代码- Private Sub Worksheet_SelectionChange(ByVal Target As Range)
- If Target.Count > 1 Then Exit Sub
- If Target.Column = 6 Then Application.CommandBars("树型菜单").ShowPopup
- End Sub
- Private Sub Worksheet_Activate() '激活Sheet2时生成左键树型菜单
- Call CreatMe
- End Sub
- Private Sub Worksheet_Deactivate() '离开Sheet2时删除左键树型菜单,防止影响其他工作表
- Call DeleteMycell
- End Sub
复制代码 |
|