将数据写入工作表单元格

时间:2018-03-01 09:07:18

标签: excel-vba vba excel

我是VBA的新手,我有这个问题。我的Excel表格上有这个命令按钮:

命令按钮:

Private Sub CommandButton1_Click()
    Dim TestCell As String
    TestCell = Range("A1").Value
    TestCell = "Test"
End Sub

我想知道为什么这段代码对我不起作用。

PS:当然我在使用Range("A1").Value = "Test"时有效,但由于某种原因,我需要使用TestCell = Range("A1").Value(用作单元格引用的变量)。

1 个答案:

答案 0 :(得分:2)

如果要引用单元格实例,请使用Range变量。然后用它来读取或写入值。

Dim ws as Worksheet
Dim TestCell As Range
Set ws = Application.ActiveSheet
Set TestCell = ws.Range("A1")
TestCell.Value = "Test"
相关问题