代码之家  ›  专栏  ›  技术社区  ›  SnakeWasTheNameTheyGaveMe

vba-调用接受父对象作为变量的函数

  •  0
  • SnakeWasTheNameTheyGaveMe  · 技术社区  · 15 年前

    是否可以创建一个接受其父对象作为变量的函数?我想说明我所说的最简单的方法是提供一个例子:

    模块1代码:

    Function IsProduct() as Boolean
        IsProduct = (vartype(Parent.Value) <> vbEmpty)
    End Function
    ' "Parent" in this case would be a Range '
    

    模块2代码:

    Dim myRng as Range
    If myRng.IsProduct Then Debug.Print "'Tis a product, sir."
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   Heinzi    15 年前

    我不完全理解你的问题;据我所知,你不能 延伸 在VBA中具有方法的现有类。也许你想要这样的东西?

    模块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
    

    模块2:

    Dim myRng as Range
    If IsProduct(myRng) Then Debug.Print "'Tis a product, sir."
    
        2
  •  1
  •   Dick Kusleika    15 年前

    您可以在类内使用me关键字来引用类。喜欢

    IsProduct = IsEmpty(Me.Value)
    

    您可以在VBA中模拟扩展本机类。创建一个名为crange的类,并给它两个属性:range和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
    

    现在,您可以使用range属性获取本机range对象的所有内置属性和方法,并创建您想要的任何其他属性(如isproduct)。