ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南

[原创] 多级菜单非递归算法/示例-全国省市行政区划三级菜单/自适应级数

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2020-4-28 18:04 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 沧海戏浪 于 2020-4-28 18:12 编辑

百度老师,我是一个VBA盲。看了您的多级菜单,太羡慕。把您的代码MAIN部分的遍历行改成了一次,本机测试发现可以快0.02秒。但是不知道这样有没有副作用。有时间的话,盼回复。
Sub main()    '????????????????????
    Dim mybar As Object, arr, i&, j&, key$, myb, pkey$
    Dim N_col As Long
    On Error Resume Next
    Set Tree = CreateObject("Scripting.Dictionary")
    Application.CommandBars("myCell").Delete
    Set mybar = Application.CommandBars.Add(Name:="myCell", position:=msoBarPopup)
    arr = Range("????!a1").CurrentRegion.Value    '??λ??????,????????????arr
    '??????,????д???????,???????????????ж????????key
    N_col = UBound(arr, 2)
    ReDim Preserve arr(1 To UBound(arr, 1), 1 To N_col + 1)    '???????????????
    '???????????????????????key
    'Exit Sub
    For i = 2 To UBound(arr)    '???????????
            If Not Tree.exists(arr(i, 1)) Then
            If arr(i, 2) = "" Then    '?????????????д?????
                AddControlButton "myCell", arr(i, 1), arr(i, 1), i, N_col
            Else    '???????????????????
                AddControlPopup "myCell", arr(i, 1), arr(i, 1)
            End If
        End If
        key = arr(i, 1)    '?????????п??
        For j = 2 To N_col  '????2-N??
            If arr(i, j) <> "" Then    '???????
                pkey = key    '?????????
                key = key & "\" & arr(i, j) '?????????
                If arr(i, j + 1) = "" Then  '?????????????д?????
                    AddControlButton pkey, key, arr(i, j), i, N_col
                Else    '???????????????????
                    If Not Tree.exists(key) Then    '????β??????
                        AddControlPopup pkey, key, arr(i, j)
                    End If
                End If
            End If
        Next
    Next
    Set mybar = Nothing
End Sub

TA的精华主题

TA的得分主题

发表于 2020-4-28 18:06 | 显示全部楼层
Sub main()    '????????????????????
    Dim mybar As Object, arr, i&, j&, key$, myb, pkey$
    Dim N_col As Long '?????????
    On Error Resume Next
    Set Tree = CreateObject("Scripting.Dictionary")    '?????洢??????
    Application.CommandBars("myCell").Delete    '??????????????
    Set mybar = Application.CommandBars.Add(Name:="myCell", position:=msoBarPopup)    '????????????
    Tree.Add "myCell", mybar
    arr = Range("????!a1").CurrentRegion.Value    '??λ??????,????????????arr
    '??????,????д???????,???????????????ж????????key
    N_col = UBound(arr, 2)
    ReDim Preserve arr(1 To UBound(arr, 1), 1 To N_col + 1)    '???????????????

    '???????????????????????key
    'Exit Sub
    For i = 2 To UBound(arr)    '???????????
            If Not Tree.exists(arr(i, 1)) Then
            If arr(i, 2) = "" Then    '?????????????д?????
                AddControlButton "myCell", arr(i, 1), arr(i, 1), i, N_col
            Else    '???????????????????
                AddControlPopup "myCell", arr(i, 1), arr(i, 1)
            End If
        End If
        key = arr(i, 1)    '?????????п??
        For j = 2 To N_col  '????2-N??
            If arr(i, j) <> "" Then    '???????
                pkey = key    '?????????
                key = key & "\" & arr(i, j) '?????????
                If arr(i, j + 1) = "" Then  '?????????????д?????
                    AddControlButton pkey, key, arr(i, j), i, N_col
                Else    '???????????????????
                    If Not Tree.exists(key) Then    '????β??????
                        AddControlPopup pkey, key, arr(i, j)
                    End If
                End If
            End If
        Next
    Next
    Set mybar = Nothing
End Sub

TA的精华主题

TA的得分主题

发表于 2020-4-28 18:07 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
沧海戏浪 发表于 2020-4-28 18:04
百度老师,我是一个VBA盲。看了您的多级菜单,太羡慕。把您的代码MAIN部分的遍历行改成了一次,本机测试发 ...

Sub main()    '????????????????????
    Dim mybar As Object, arr, i&, j&, key$, myb, pkey$
    Dim N_col As Long '?????????
    On Error Resume Next
    Set Tree = CreateObject("Scripting.Dictionary")    '?????洢??????
    Application.CommandBars("myCell").Delete    '??????????????
    Set mybar = Application.CommandBars.Add(Name:="myCell", position:=msoBarPopup)    '????????????
    Tree.Add "myCell", mybar
    arr = Range("????!a1").CurrentRegion.Value    '??λ??????,????????????arr
    '??????,????д???????,???????????????ж????????key
    N_col = UBound(arr, 2)
    ReDim Preserve arr(1 To UBound(arr, 1), 1 To N_col + 1)    '???????????????

    '???????????????????????key
    'Exit Sub
    For i = 2 To UBound(arr)    '???????????
            If Not Tree.exists(arr(i, 1)) Then
            If arr(i, 2) = "" Then    '?????????????д?????
                AddControlButton "myCell", arr(i, 1), arr(i, 1), i, N_col
            Else    '???????????????????
                AddControlPopup "myCell", arr(i, 1), arr(i, 1)
            End If
        End If
        key = arr(i, 1)    '?????????п??
        For j = 2 To N_col  '????2-N??
            If arr(i, j) <> "" Then    '???????
                pkey = key    '?????????
                key = key & "\" & arr(i, j) '?????????
                If arr(i, j + 1) = "" Then  '?????????????д?????
                    AddControlButton pkey, key, arr(i, j), i, N_col
                Else    '???????????????????
                    If Not Tree.exists(key) Then    '????β??????
                        AddControlPopup pkey, key, arr(i, j)
                    End If
                End If
            End If
        Next
    Next
    Set mybar = Nothing
End Sub

TA的精华主题

TA的得分主题

发表于 2020-5-3 01:44 | 显示全部楼层
dogingate 发表于 2018-7-9 00:04
我这个还没有做到自适应,主要是针对行政划分这个三级菜单而修改的代码,原代码加载时间为11.31s,修改之后 ...

实测,当上级列输入内容不在菜单中时,会报错。比如第一列输入的是武汉,但是武汉在一级菜单中是没有的,点第二列的时候,就会报错。其实三级菜单已经够用了,用不了自适应。

TA的精华主题

TA的得分主题

发表于 2020-6-2 17:20 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2020-9-2 11:15 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2020-12-24 21:36 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2021-5-1 10:35 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
学习为了成长,收获必须分享。感谢楼主无私奉献!
修改为六级行政区划会报错,强制纠错后,加载菜单用时两分钟。
QQ图片20210501101844.png

VBA-行政划分多级菜单.part01.rar

2 MB, 下载次数: 73

VBA-行政划分多级菜单.part02.rar

2 MB, 下载次数: 71

VBA-行政划分多级菜单.part03.rar

2 MB, 下载次数: 69

VBA-行政划分多级菜单.part04.rar

2 MB, 下载次数: 66

VBA-行政划分多级菜单.part05.rar

2 MB, 下载次数: 70

VBA-行政划分多级菜单.part06.rar

2 MB, 下载次数: 73

VBA-行政划分多级菜单.part07.rar

2 MB, 下载次数: 77

VBA-行政划分多级菜单.part08.rar

2 MB, 下载次数: 70

VBA-行政划分多级菜单.part09.rar

209.02 KB, 下载次数: 66

TA的精华主题

TA的得分主题

发表于 2021-7-21 12:54 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2021-9-23 07:47 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-16 03:40 , Processed in 0.048468 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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