|
楼主 |
发表于 2016-4-8 10:21
|
显示全部楼层
本帖最后由 一指禅62 于 2016-4-24 12:20 编辑
全部代码(模块代码)- Option Explicit
- Dim CBar As CommandBar
- Sub CommandBarAdd()
- Dim arr, i&
- arr = Sheet4.Range("A1").CurrentRegion
- Call DelBar
- Set CBar = Application.CommandBars.Add("树型菜单", msoBarPopup)
- For i = 2 To UBound(arr)
- Call AddNewMenu(CStr(arr(i, 1)), arr(i, 2))
- Next
- End Sub
- Private Sub AddNewMenu(ByVal key As String, ByVal txt As String)
- Dim j%, iPath$, n%
- Dim FindMenu As CommandBarControl
- Dim NewMenu As CommandBarControl
- On Error Resume Next
- n = WorksheetFunction.CountIf(Sheet4.Range("A:A"), key & "*")
- j = IIf(n = 1, msoControlButton, msoControlPopup)
- Set FindMenu = CommandBars.FindControl(Tag:=CStr(Left(key, Len(key) - 2))) '查找这个父级
- On Error GoTo 123
- If FindMenu Is Nothing Then '没有父级
- iPath = txt '路径
- Set NewMenu = CBar.Controls.Add(Type:=j)
- Else
- iPath = FindMenu.DescriptionText & "" & txt
- 'iPath = FindMenu.DescriptionText & txt '如果输出在一个单元格则用这句代码
- Set NewMenu = FindMenu.Controls.Add(Type:=j)
- End If
- With NewMenu
- .BeginGroup = True
- .Caption = txt
- .Tag = key
- .DescriptionText = iPath
- .OnAction = IIf(j < 2, "'输出到单元格 """ & iPath & """'", "")
- End With
- Set NewMenu = Nothing
- Exit Sub
- 123:
- FindMenu.Delete
- End Sub
- Sub 输出到单元格(iStr As String)
- Dim a: a = Split(iStr, "")
- If Len(ActiveCell.Value) > 0 Then
- If MsgBox("是否替换这条记录?", vbQuestion + vbYesNo) = vbNo Then Exit Sub
- End If
- ActiveCell.Resize(1, 15) = ""
- ActiveCell.Resize(1, UBound(a) + 1) = a
- End Sub
- Sub DelBar()
- For Each CBar In CommandBars
- If CBar.Name = "" Or CBar.Name Like "树型菜单*" Then CBar.Delete
- Next
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|