码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • VBA -[知识点]: 字典


    VBA字典

    • 记录一次VBA字典的使用
      • 部分代码
    • 1. VBA 中创建一个字典对象
    • 2. 字典对象添加key 和 value
    • 3. 获取字典对象的key list
    • 4. 判断是否存在某个key
    • 5. 修改字典值
    • 6. 修改字典key 名称
    • 6. 删除某个item
    • 7. 删除所有item

    记录一次VBA字典的使用

    部分代码

    Public Sub MainRun()
        '1. 获取数据集,存储到字典中
        Set dict = CreateObject("Scripting.dictionary")
        Set dictUse = CreateObject("Scripting.dictionary")
        
        Sheets("数据").Select
        Range("J3").Select
        Range(Selection, Selection.End(xlToRight)).Select
        ColumnsNum = Selection.Count
        
        For i = 1 To ColumnsNum
            k = Cells(3, i + 9) & "-" & Cells(5, i + 9) & "-" & Cells(6, i + 9) & "-" & Cells(7, i + 9)
            v = Cells(8, i + 9)
            dict.Add k, v
        Next i
        '2. 获取Key list
        KeyList = dict.keys
        startColumns = 10
        
        '3. 遍历Key list进行数据运算
        For Each KeyItem In KeyList
            shortKey = Left(KeyItem, Len(KeyItem) - 1)
            key3Field = Left(shortKey, Len(shortKey) - 1)
            If Not dictUse.exists(key3Field) Then
                Sheets("控件").Cells(3, startColumns) = key3Field
                dictUse.Add key3Field, key3Field
                aSuffix = Right(KeyItem, 1)
                For i = 5 To 8 Step 1
                    steps = Sheets("控件").Cells(i, 6)
                    oneKey = shortKey & steps
                    anotherSteps = Sheets("控件").Cells(i, 7)
                        anotherKey = shortKey & anotherSteps
                    ' 保证两个值都存在的情况下进行数据运算
                    If dict.exists(anotherKey) And dict.exists(oneKey) Then
                        Debug.Print dict(oneKey) - dict(anotherKey)
                        Sheets("控件").Cells(i, startColumns) = dict(oneKey) - dict(anotherKey)
                    End If
                Next
                startColumns = startColumns + 1
            End If
            
        Next
        
        Sheets("控件").Select
    End Sub
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    1. VBA 中创建一个字典对象

    Set dict = CreateObject("Scripting.dictionary")

    2. 字典对象添加key 和 value

    dict.Add key,value

    3. 获取字典对象的key list

    dict.keys

    4. 判断是否存在某个key

    dict.exists(key)

    5. 修改字典值

    dict(key) = newdata

    6. 修改字典key 名称

    dict.key(key) = newkeyname

    6. 删除某个item

    dict.remove key

    7. 删除所有item

    dict.removeall
    运行结果:
    在这里插入图片描述
    >>> 如果你觉得我的文章对你有用,不妨 【点赞】 加 【关注】,你的支持是我持续写作的动力,thank you! <<<

  • 相关阅读:
    JSON 配置文件
    Vue3根据搜索框内容跳转至本页面指定位置
    《痞子衡嵌入式半月刊》 第 58 期
    node
    数据结构学习系列之顺序表的查找与排序以及去重
    Vue 3 和 Spring Boot 3 的操作流程和执行步骤详解
    工程制图点的投影练习
    GameFramework:Resource加载,资源加载,依赖加载,任务池,对象池,引用计数
    如何编写vscode插件-- 全声明周期记录
    Python基础(第五期): python数据容器(序列) && 列表 && 集合 && 元素 && 字符串 && 字典 && 序列遍历操作
  • 原文地址:https://blog.csdn.net/qq_37608398/article/details/126825683
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号