当单元格更改时,将复制和粘贴操作偏移到同一行的不同单元格

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

我使用此代码来偏移单元格更改后的时间戳。

私有子工作表_更改(ByVal 目标作为范围)

如果不相交(目标,范围(“b2:b888”))则什么都没有 出错时继续下一步

如果 Target.Value = "" 那么

目标.Offset(0, 1) = ""

其他

Target.Offset(0, 1).Value = Format(Now, "HH:mm")

结束如果 结束如果

结束子

现在我想从该行的时间戳更改为特定值。 因此,在编辑“b2”时,它将粘贴到“w2”中的“c2”值。

不知道如何通过每行的偏移来顺利完成此操作,因为不需要为每个单元格单独的代码。

谢谢你

我尝试组合复制粘贴代码而不是时间戳偏移,但似乎没有什么可以很好地粘在一起。

timestamp offset
1个回答
0
投票

明白了,

私有子工作表_Change(ByVal Target As Range) Dim AffectedRange 作为范围 昏暗单元作为范围

' Check if the changed cell is in column B
Set AffectedRange = Intersect(Target, Me.Columns("B"))
If Not AffectedRange Is Nothing Then
    Application.EnableEvents = False ' To prevent triggering the event again
    
    ' Loop through each changed cell in column B
    For Each Cell In AffectedRange
        ' Copy value from column W to corresponding cell in column C
        Me.Cells(Cell.Row, "C").Value = Me.Cells(Cell.Row, "W").Value
    Next Cell
    
    Application.EnableEvents = True ' Re-enable events
End If

结束子

© www.soinside.com 2019 - 2024. All rights reserved.