gpt4 book ai didi

vba - Excel VBA - 工作表属性未按应有的方式分配字段

转载 作者:行者123 更新时间:2023-12-03 03:29:47 31 4
gpt4 key购买 nike

VBA 新手 - 尝试使用工作表属性,就像在 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 设置为 Public,那么它会按预期工作,但这首先违背了使用 Properties 的目的。这可能很简单,我不理解 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

关于vba - Excel VBA - 工作表属性未按应有的方式分配字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45870595/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com