Is it possible to create a function which accepts it's parent object as a variable? I suppose the simple开发者_开发百科st way to illustrate what I'm talking about is to provide an example:
Module 1 Code:
Function IsProduct() as Boolean
IsProduct = (vartype(Parent.Value) <> vbEmpty)
End Function
' "Parent" in this case would be a Range '
Module 2 Code:
Dim myRng as Range
If myRng.IsProduct Then Debug.Print "'Tis a product, sir."
I did not fully understand your question; as far as I know you cannot extend existing classes with methods in VBA. Maybe you want something like this?
Module 1:
Function IsProduct(parent As Object) as Boolean
IsProduct = (vartype(parent.Value) <> vbEmpty)
End Function
' This can take a Range as well as other objects as a parameter
Module 2:
Dim myRng as Range
If IsProduct(myRng) Then Debug.Print "'Tis a product, sir."
You would use the Me keyword inside a class to refer to the class. Like
IsProduct = IsEmpty(Me.Value)
You can simulate extending native classes in VBA. Create a class called cRange and give it two properties: Range and IsProduct
Private mclsRange As Range
Private Sub Class_Terminate()
Set mclsRange = Nothing
End Sub
Public Property Get Range() As Range
Set Range = mclsRange
End Property
Public Property Set Range(clsRange As Range)
Set mclsRange = clsRange
End Property
Public Property Get IsProduct() As Boolean
IsProduct = IsEmpty(Me.Range.Value)
End Property
Now you can use the Range property to get to all the built in properties and methods of the native Range object, and create any other properties (like IsProduct) that you want.
精彩评论