|
|
#8
扑腾2023-10-31 21:25
回复 6楼 牛掰
Excel表中的三级菜单代码如下:
Sub CreatMe() '生成左键树型菜单
Dim d As Object, i&, j&, k, k2, t, a, l&, arr, x As Object
Application.ScreenUpdating = False
Set d = CreateObject("scripting.dictionary")
arr = Sheets("货品资料").[B2].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) & " " & arr(i, 4) & " " & arr(i, 5)
Next
k = d.keys '一级分类
On Error Resume Next
("树型菜单").Delete '删除可能存在的
With ("树型菜单", 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), "", "'显示在活动单元格 """ & k(i) & "," & k2(j) & """'")
For l = 1 To UBound(a)
If Len(a(l)) Then
pms = Split(a(l)): ''''''''''''''分割显示:品名,条码,单位
pm = pms(0): ' 品名
pm1 = pms(1): ' 条码
pm2 = pms(2) ' 单位
With .Controls.Add(Type:=msoControlButton)
.BeginGroup = True
.Caption = pm
.OnAction = "'显示在活动单元格 """ & k(i) & "," & k2(j) & "," & pm & "," & pm1 & "," & pm2 & """'"
End With
End If
Next
End With
Next
End With
Next
End With
Application.ScreenUpdating = True
End Sub
Sub 显示在活动单元格(s$)
Dim a: a = Split(s, ",")
ActiveCell.Resize(1, 5) = a
End Sub
移植到vb6.0的窗体中后,红色代码就不起作用了,点击菜单选项时数据无法写入表单控件中。
|