工作表变更


1

我正在尝试将一个宏放在一个时间戳中。

我有2张纸inputdashboard

当单元格A2 input更改时,我希望现在在单元格M8上使用该函数dashboard

我在工作input表上按alt + F11调出vba编辑器并使用:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$A$2" Then

        Sheets("Dashboard").Select
        Range("M8").Select
        ActiveCell.FormulaR1C1 = "=NOW()"
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False

    End If

End Sub

它在以下行失败:

Range("M8").Select

代码有什么问题?

Answers:


2

这是因为在工作表代码中,如果您未将父级指定为该范围,则它将假定代码所在的工作表不像模块那样是活动工作表。因此,input不再处于活动状态,您将无法在该工作表中选择单元格。

您可以用以下一行替换IF中的所有内容:

Worksheets("Dashboard").Range("M8").Value2 = Now()

它将当前日期/时间粘贴为一个值,而不是公式,并且无需选择或激活任何内容。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.