VBA:获取变量名称

2024-01-01

是否有函数或属性可以获取变量名称?

就像是

msgBox myVariable.name or

msgBox nameOfVariable(myVariable)

返回"myVariable"当我用例如定义它时myVariable = "whatever"? 谷歌只是给变量引用带来了问题......


可能的类方法如下(已评论):

  1. 添加类模块

    在 VBA 集成开发环境中

    • 单击插入->类模块

    • 单击视图 -> 属性窗口

    • enter (Name)属性文本框并输入“变量”(或您可能喜欢的任何内容,但要与以下步骤保持一致)

    • 在中输入以下代码Class代码窗格

      Option Explicit
      
      'declare the fields that will be attached to every instance of this class
      Public name As String '<--| this will store the name of the variable to which you'll set the object of this class
      Public value As Variant '<--| this will store the value associated with the variable to which you'll set the object of this class
      
      'declare a `method` to write the `value` of the object in the named range named after the `name` field 
      Sub WriteRange(Optional wb As Variant) '<--| you can pass the reference of the workbook whose named ranges you want to exploit
         If IsMissing(wb) Then Set wb = ActiveWorkbook '<--| if no workbook reference is passed then the currently active workbook is assumed
         If TypeName(wb) = "Workbook" Then '<-- check for a proper workbook reference being passed)
              On Error Resume Next '<-- to prevent unassigned named range throw an error
              wb.Names(name).RefersToRange.value = value '<--| write the  value of the `value` filed of the current instance in the named range of the passed workbook named after the `name` filed of the current instance
          End If
      End Sub 
      
  2. Exploit Variable代码中的类

    作为利用的一个例子Variable三个变量的类,例如String第一个值,一个Integer第二个和 a 的值Double对于第三个值,在任何模块代码窗格中输入以下代码:

        Option Explicit
    
        Sub main()
            Dim myVariable1 As Variable, myVariable2 As Variable, myVariable3 As Variable '<--| declare your variables of type "Variable": choose whatever name you may want
    
            Set myVariable1 = CreateVariable("myVariable1", "this is a string value") '<-- set your 1st variable with its name (you must use the same name as the variable!) and value (myVariable1 will have a `String`type value)
            Set myVariable2 = CreateVariable("myVariable2", 10) '<-- set your 2nd variable with its name (you must use the same name as the variable!) and value (myVariable2 will have a `Integer`type value)
            Set myVariable3 = CreateVariable("myVariable3", 0.3)'<-- set your 3rd variable with its name (you must use the same name as the variable!) and value (myVariable3 will have a `Double` type value)
    
            'exploit `WriteRange` method of your Class to write the object value into the corresponding named range: you must have set proper named ranges in your currently active workbook
            myVariable1.WriteRange '<--| this will write the string "this is a string value" in named range "myVariable1" of your currently active workbook
            myVariable2.WriteRange '<--| this will write the number '10' in named range "myVariable2" of your currently active workbook
            myVariable3.WriteRange '<--| this will write the number '0.3' in named range "myVariable3" of your currently active workbook 
        End Sub
    
        ' helper Function to create an object of `Variable` class and initialize its `name` and `value` properties 
        Function CreateVariable(name As String, value As Variant) As Variable '<--| the function returns an object of `Variable` class
            Set CreateVariable = New Variable '<--| this creates a new object of `Variable` class
            With CreateVariable '<--| refer to the newly created object and ...
                .name = name '<--| ... set its `name` property ...
                .value = value '<--| ... and its `value` property
            End With
        End Function
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

VBA:获取变量名称 的相关文章

  • 当使用公式生成超链接时,VBA 打开 Excel 超链接不起作用

    使用公式生成的 Excel 超链接似乎存在错误 我使用的是 Excel 2010 我有一个电子表格 其中的单元格包含 URL 我的目标是执行以下两件事 将这些单元格变成超链接 创建一个键盘快捷键来打开这些超链接 这样我就不必使用鼠标了 为了
  • Excel工作簿关闭后反复打开

    我使用了 Application ontime 方法来调度一些宏 关闭工作簿后 它会一次又一次地打开 为了解决这个问题 我在工作簿上设置了另一个事件 BeforeClosed 现在它显示运行时错误 1004 Object Applicati
  • 检查对以下内容的理解:“变量”与“变量” “价值”、“功能”与“抽象”

    这个问题是后续问题this one https stackoverflow com questions 25327705 is function a sort of variable 25329157 25329157在学习 Haskell
  • VSTO 替代方案 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 VSTO 有哪些替代方案 ManagedXll 能做什么而 VSTO 不能 你什么时候使用其中一个而不
  • Excel 工作表名称的有效字符

    在 Java 中 我们使用以下包以编程方式创建 Excel 文档 org apache poi hssf 如果您尝试设置工作表的名称 不是文件 而是内部 Excel 工作表 在以下情况下您将收到错误消息 名称超过 31 个字符 该名称包含以
  • Redim Preserve 给出“下标超出范围”

    我想要Redim Preserve一个数组我不断收到错误 下标超出范围 我知道只有最后一个维度的大小可以更改 这正是我正在做的事情 这里出了什么问题 数组的类型是Variant BmMatrix Sheets BENCH Range a60
  • php如何生成动态list()?

    根据我的理解 这就是 list 的工作原理 list A1 A2 A3 array B1 B2 B3 所以在帮助下list 我们可以相应地从数组中分配值 这是我的问题 如何生成动态list 1 基于数据库返回结果 我不确定有多少 但我将其全
  • Excel 2013 数据透视表不会更改当前页面,除非手动导航到

    我们有一小段 VBA 代码 多年来一直完美运行 本质上是 Me PivotTables APivot PivotFields AField CurrentPage Some text 这种方法一直有效 直到 Excel 2013 该行将失败
  • 如何通过电子邮件发送 Excel 文件?

    我有一个 excel 文件 Excel 2003 xls 格式 我想用 c 通过电子邮件发送它 我的代码成功发送它 但是当我尝试打开响应文件时 它似乎编码错误 例如 这里是响应文件名 utf 8 B RWxzesOhbW9sw6FzXzIw
  • 使用 ClosedXML 创建数据透视表

    我正在尝试使用 ClosedXML V0 91 1 创建数据透视表 但我不断遇到问题 因为我的 Excel 文件包含不可读的内容 然后 Excel 工作簿在单击时删除了我的数据透视表Yes below 下面是我击中时的显示Yes 它正在删除
  • 通过 Excel / VBA 调用 DLL 中的 C++ 函数在传递双参数时生成异常

    我试图通过 DLL 在 Excel VBA 中使用 C C 静态函数 我在 VS17 中调试时遇到异常 我怀疑这是参数传递方式的问题 它是双精度 EXCEL EXE 中 0x00007FFA28BBA14F kernel32 dll 处抛出
  • 是否存在用于开放 xml Excel 编辑的良好包装类和/或库?

    我正在寻找一个不错的库 用于在我们的 Windows 服务器上编辑和 或生成 Excel 文档 我觉得 open xml sdk 可能是可行的方法 但对我来说 学习曲线似乎很陡峭 而且我们的开发时间有限 我认为编辑 Excel 文档不应该那
  • 如何使用 php 将 *.xlsb 转换为数组或 *.csv

    我正在尝试转换 xlsb文件到php array or csv文件 或至少 xls 我尝试使用PHPExcel 但看起来它无法识别该文件中的内容 我注意到 你可以重命名 xlsb文件到 zip文件 然后使用命令行解压缩unzip zip 之
  • Access / Word 2010 VBA 邮件合并尝试打开 [文件夹名称].mdb 而不是 ACCDB 源

    我们正在尝试从 Access 中自动执行邮件合并过程 单击按钮后 VBA 将运行指定当前数据库 accdb 作为数据源并运行 SQL 具体代码如下 Set up Word Dim objWord As Object Set objWord
  • 实例化 Microsoft.Office.Interop.Excel.Application 对象时出现错误:800700c1

    实例化 Microsoft Office Interop Excel Application 以从 winforms 应用程序生成 Excel 时 出现以下错误 这之前是有效的 但突然间它停止工作了 尽管代码和 Excel 版本没有变化 我
  • VBA (Excel) 中 =Empty 和 IsEmpty() 有什么区别?

    我使用了以下 VBA 代码 Do While Cell i 1 lt gt Empty doing things i i 1 Loop 在 Excel 中迭代列 具有双精度 整数值 然后我发现了一种情况 每当单元格的值为 0 时 测试的计算
  • Excel 的 VBA - 如何检查范围的交集不为空

    我有两个问题 1 如何检查交集或范围是否不为空 例如 如果我想检查它是否为空 我会写 if application intersect r1 r2 is nothing 但有什么东西是否定虚无的吗 例如 并非没有任何事情不起作用 2 如何比
  • 如何区分列表框中显示的文本和实际值?

    我有一个带有多选选项的列表框 我使用以下方式填充它addItem功能 我在 Google 上找不到任何有关此内容的文章 但我需要区分列表框中显示的文本和实际值 例如 shown hiddenvalue monday A1 tuesday A
  • 在用户窗体终止/关闭 VBA 时调用数组

    我有一个问题 我想在用户窗体关闭时将用户窗体的内容存储在数组中 我认为我的语法正确 但似乎不会在用户窗体初始化时重新填充 我尝试将数组放入其自己的模块中 但这也不起作用 有人愿意启发我吗 示例代码 Public Sub DPArrayStu
  • 如何检查python xlrd库中的excel文件是否有效

    有什么办法与xlrd库来检查您使用的文件是否是有效的 Excel 文件 我知道还有其他库可以检查文件头 我可以使用文件扩展名检查 但为了多平台性我想知道是否有任何我可以使用的功能xlrd库本身在尝试打开文件时可能会返回类似 false 的内

随机推荐