Excel VBA相当于Word变量集合



Excel VBA (Office 16)是否具有与Word VBAActiveDocument.Variables集合相等或类似的属性?我想在文档中保存项目值,而不必使用隐藏工作表。

可以使用Name对象永久保留所需的值。Name对象通常指一个单元格或一组单元格,甚至指整个工作表。但它也可以用来存储公式,这样也可以保存数据。

如果您使用此特性,建议您命名变量名,以便除了其他name对象之外,还可以清楚地识别它们。此外,您可以隐藏它们以避免误用。参见下面的示例代码:

Sub fnCreateNamesAsVariablesAndHideThem()
'the False parameter hides the name to the front-end
With ThisWorkbook.Names
.Add "var_intNumber", 7, False
.Add "var_strTitle", "Gone with the Wind", False
.Add "var_LngPtr", 1127654, False
End With
Debug.Print Names("var_strTitle")
Debug.Print Names("var_LngPtr")
'you can use them in a cell, like: =var_LngPtr, even hidden.
End Sub

重要的是要注意,在创建Name时,不会计算另一个Name是否存在:它将始终被覆盖。

在网上有提到其他方法来实现这一点,如写入Windows注册表,创建一个"自定义属性",等等。它们的制作和阅读都比较费力。

最新更新