将数据写入工作表单元格

问题描述 投票:1回答:1

我是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(变量用作单元格引用)。

excel-vba vba excel
1个回答
2
投票

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

Dim ws as Worksheet
Dim TestCell As Range
Set ws = Application.ActiveSheet
Set TestCell = ws.Range("A1")
TestCell.Value = "Test"
© www.soinside.com 2019 - 2024. All rights reserved.