VBA 新手——尝试使用 Sheet 属性,就像在 OOP 语言中使用 Getters 和 Setters 一样。我在 Sheet1 中有以下代码(为了清晰起见,进行了简化):
Option Explicit
Private bAllowChange As Boolean
Public Property Let AllowChange(bLetAllowChange As Boolean)
bAllowChange = bLetAllowChange
End Property
我希望当AllowChange
属性由另一个模块中的子程序调用,bAllowChange
字段将相应更新。通过使用VBE的调试功能我可以看到AllowChange
正在传递正确的值(bLetAllowChange
采取正确的值)但行
bAllowChange = bLetAllowChange
不将值分配给bAllowChange
。如果我设置bAllowChange
公共然后它按预期工作,但这首先违背了使用属性的目的。这可能很简单,我不理解 VBA 中的范围。有什么建议么?提前致谢。
如果您想要持久属性,请尝试此(只是如何设置/获取/更新/删除自定义属性的示例)
Sub customProperties()
ActiveWorkbook.Sheets("Sheet1").customProperties.Add "abc123", 123
Debug.Print ActiveWorkbook.Sheets("Sheet1").customProperties(1) ' custom properties are not indexed by name
ActiveWorkbook.Sheets("Sheet1").customProperties(1).Value = "this is my data"
Debug.Print ActiveWorkbook.Sheets("Sheet1").customProperties(1)
ActiveWorkbook.Sheets("Sheet1").customProperties(1).Delete
' CustomDocumentProperties Types
' msoPropertyTypeBoolean 2
' msoPropertyTypeDate 3
' msoPropertyTypeFloat 5
' msoPropertyTypeNumber 1
' msoPropertyTypeString 4
ActiveWorkbook.CustomDocumentProperties.Add Name:="xyz", LinkToContent:=False, Type:=msoPropertyTypeString, Value:="xyz"
Debug.Print ActiveWorkbook.CustomDocumentProperties("xyz")
ActiveWorkbook.CustomDocumentProperties("xyz").Value = "abcdefg"
Debug.Print ActiveWorkbook.CustomDocumentProperties("xyz")
ActiveWorkbook.CustomDocumentProperties("xyz").Delete
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)