我是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
(变量用作单元格引用)。
如果要引用单元实例,请使用Range变量。然后用它来读取或写入值。
Dim ws as Worksheet
Dim TestCell As Range
Set ws = Application.ActiveSheet
Set TestCell = ws.Range("A1")
TestCell.Value = "Test"