VBA中公共变量的替代方案

use*_*044 5 excel vba excel-vba

我有几个模块引用的公共变量.我知道如果你调试或点击停止变量被清除.我一直把这些变量写到电子表格中,所以如果它们被清除,我会把它们弄清楚,但这很麻烦.我宁愿让它全部存储它代码.是否有任何替代公共变量永远不会被清除?

Dav*_*ens 10

下面是CustomDocumentProperties的一个示例,我最近开始使用它来存储一些元信息(比处理CustomXMLParts更容易).

下面的示例仅存储字符串数据,但您也可以使用日期,数字和是/否(可以使用一些finagling作为布尔值).字符串数据限制为255个字符.

   Sub Test()
   '## Assign a CDP
   SetCustomProperty "myProperty", "some value I want to store"

   End Sub
Run Code Online (Sandbox Code Playgroud)

您可以从Backstage中查看CPD 信息| 属性| 高级属性| 自定义:

在此输入图像描述

如果是End运行时,可以从CDP恢复值,可以通过以下方式查询属性值:

myVar = ActiveWorkbook.CustomDocumentProperties("myProperty").Value

您可以使用这些函数来设置集合中的属性CustomDocumentProperties:

Sub SetCustomProperty(property$, val$)
    Dim cdp As Variant
    Dim hasProperty As Boolean
    If HasCustomProperty(property) Then
        ActiveWorkbook.CustomDocumentProperties(property).Value = val
    Else
        ActiveWorkbook.CustomDocumentProperties.Add property, False, msoPropertyTypeString, val

    End If
End Sub
Private Function HasCustomProperty(property$) As Boolean
Dim cdp As Variant
Dim boo As Boolean
For Each cdp In ActiveWorkbook.CustomDocumentProperties
    If cdp.name = property Then
        boo = True
        Exit For
    End If
Next
HasCustomProperty = boo
End Function
Run Code Online (Sandbox Code Playgroud)

  • +1 - 我的首选方法.值得注意的是,除名称外,还有一个隐藏的命名空间,可以在电子表格用户隐藏名称时使用.(http://www.cpearson.com/excel/hidden.htm中的更多信息) (3认同)

Ric*_*ica 7

一个简单的解决方案是将变量存储在注册表中,并根据需要进行读/写.这具有在多个Excel会话中保留值的额外好处(甚至在计算机重新启动或崩溃之后 - 假设您的注册表幸存下来了!).

编辑:有关此问题的更多信息,请参阅John Walkenbach的书.

编辑:请参阅Ioannis的以下评论作为重要考虑因素.


Boilerplate警告: 这里有龙,Twiddle与Windows注册表在你的危险等等.


尽管如此,上述警告已经意识到,Windows计算机上的几乎每个程序都会对注册表执行某些操作,并且这样做本身并不危险.只需确保您的代码仅更改/删除由Excel应用程序创建的注册表项.


使用Windows Scripting的示例程序(我没有写这些;来自快速搜索):

从书记官处阅读:

'reads the value for the registry key i_RegKey
'if the key cannot be found, the return value is ""
Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object

  On Error Resume Next
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'read key from registry
  RegKeyRead = myWS.RegRead(i_RegKey)
End Function
Run Code Online (Sandbox Code Playgroud)

检查注册表项是否存在:

'returns True if the registry key i_RegKey was found
'and False if not
Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object

  On Error GoTo ErrorHandler
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'try to read the registry key
  myWS.RegRead i_RegKey
  'key was found
  RegKeyExists = True
  Exit Function

ErrorHandler:
  'key was not found
  RegKeyExists = False
End Function
Run Code Online (Sandbox Code Playgroud)

保存注册表项:

'sets the registry key i_RegKey to the
'value i_Value with type i_Type
'if i_Type is omitted, the value will be saved as string
'if i_RegKey wasn't found, a new registry key will be created
Sub RegKeySave(i_RegKey As String, _
               i_Value As String, _
      Optional i_Type As String = "REG_SZ")
Dim myWS As Object

  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'write registry key
  myWS.RegWrite i_RegKey, i_Value, i_Type

End Sub
Run Code Online (Sandbox Code Playgroud)

从注册表中删除密钥:

'deletes i_RegKey from the registry
'returns True if the deletion was successful,
'and False if not (the key couldn't be found)
Function RegKeyDelete(i_RegKey As String) As Boolean
Dim myWS As Object

  On Error GoTo ErrorHandler
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'delete registry key
  myWS.RegDelete i_RegKey
  'deletion was successful
  RegKeyDelete = True
  Exit Function

ErrorHandler:
  'deletion wasn't successful
  RegKeyDelete = False
End Function
Run Code Online (Sandbox Code Playgroud)