我正在编写的Excel/VBA类中的属性返回一个Range.我使用http://www.cpearson.com/excel/DefaultMember.aspx中描述的技术使其成为该类的默认属性.我希望将所有Range类的内置属性和方法与我的类的对象一起使用,而无需显式指定属性.它不起作用.这里有几个更简单的类来说明.(这些列表是使用文本编辑器查看的导出源,因为VBA的编辑器隐藏了Attribute语句.)
' clsDefLong: This class just verifies that default properties work as I expected.
Public Property Get DefProp() As Long
Attribute DefProp.VB_UserMemId = 0
DefProp = 125
End Property
' clsDefRange: This class is identical except the default property returns a Range.
Public Property Get DefProp() As Range
Attribute DefProp.VB_UserMemId = 0
Set DefProp = ActiveCell
End Property
Run Code Online (Sandbox Code Playgroud)
这是一个普通模块中的Sub,用于实例化和测试类.评论表明当我单步执行时会发生什么:
Sub DefTest()
Dim DefRange As New clsDefRange, DefLong As New clsDefLong
Debug.Print DefLong.DefProp '(1) Displays 125. Verifies the class …Run Code Online (Sandbox Code Playgroud)