从macros复制数据时,需要在另一个单元格中设置“现在函数”

我有这个macros从剪贴板获取数据,并将其粘贴到一个特定的单元格转置一些信息。

Sub UpdateData() '~~> Change this to the relevant sheet Set ws = ThisWorkbook.Sheets("Sheet2") With ws '~~> Using this as you are copying it from Notepad~~~~ .Activate .Range("H1").Select .PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Worksheets("Sheet2").Range("H1" & ",H3").Copy Sheets("Sheet2").Range("C" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Transpose:=True '~~Clear data content~~~~~~~~~~~~~~~~~~~~~~~~~ Range("H1:H10").ClearContents End With End Sub 

我需要使用NOW公式更新(C)行旁边的单元格B来更新单元格B。

我有这个其他macros更新行B,每当行C更新,但他们不一起工作。

 Sub Worksheet_Change(ByVal Target As Range) Application.MoveAfterReturn = True If Target.Count = 1 Then If Not Intersect(Target, Range("C1:C1000")) Is Nothing Then Cells(Target.Row, "B") = Now End If End If End Sub 

任何想法,我应该怎么做?

目标是已更改并触发Worksheet_Change事件macros的单元格。 在你的情况下,这是多个单元格,所以你必须分别处理每个单元格。

 Sub Worksheet_Change(ByVal Target As Range) Application.MoveAfterReturn = True If Not Intersect(Target, Range("C1:C1000")) Is Nothing Then On Error GoTo bm_Safe_Exit Application.EnableEvents = False Dim c As Range For Each c In Intersect(Target, Range("C1:C1000")) Cells(c.Row, "B") = Now Next c End If bm_Safe_Exit: Application.EnableEvents = True End Sub 

在更改数据(添加timerstamp)到工作表时closures事件处理,或者触发Worksheet_Change在其上运行。

确定C1:C1000范围内的一个或多个单元格已被更改后, For Each … Next Statement循环遍历每个单元格,并在该行的列B上存放时间戳。