我已经构建了一个类,需要将数据记录到单元格中.因此我写了一个这样做的函数.代码如下:
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不是一个对象吗?