小编new*_*240的帖子

范围和变量之间的差异作为范围变暗

我已经构建了一个类,需要将数据记录到单元格中.因此我写了一个这样做的函数.代码如下:

Option Explicit

Private sName As String

Public Property Let Name(ByVal strValue As String)
    sName = strValue
End Property
Public Property Get Name() As String
    Name = sName
End Property

Public Function ItemToCell(ByRef tgtCell As Range)
    tgtCell = sName
End Function
Run Code Online (Sandbox Code Playgroud)

我还设置了一个按钮来触发这个过程:

Private Sub CommandButton1_Click()
    Dim tmpData As New MyClass
    tmpData.Name = "Tom"

    Dim tgtCell As Range
    Set tgtCell = Worksheets("Sheet1").Range("A1")

    'Method 1, this failed with error 424
    tmpData.ItemToCell (tgtCell)

    'Method 2, it works
    tmpData.ItemToCell (Worksheets("Sheet1").Range("B1"))
End Sub
Run Code Online (Sandbox Code Playgroud)

我认为这两种方法是相同的,但显然它们不是.为什么?变量tgtCell不是一个对象吗?

excel vba

4
推荐指数
1
解决办法
308
查看次数

标签 统计

excel ×1

vba ×1