如何用静态日期填充 2 列

发布于 2025-01-11 13:01:04 字数 873 浏览 0 评论 0原文

我使用以下代码,用于在 B 列中填充数据时在 C 列中填充静态日期。

如果在 D 列中填充数据,我还希望在 E 列中填充静态日期。请告知 tq

Private Sub Worksheet_Change(ByVal Target As Range) 
    'Update 20140722 
    Dim WorkRng As Range 
    Dim Rng As Range Dim xOffsetColumn As Integer 
    Set WorkRng = Intersect(Application.ActiveSheet.Range("B:B"), Target) 
    xOffsetColumn = 1 
    If Not WorkRng Is Nothing Then 
        Application.EnableEvents = False 
        For Each Rng In WorkRng 
            If Not VBA.IsEmpty(Rng.Value) Then 
                 Rng.Offset(0, xOffsetColumn).Value = Now 
                 Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss" 
            Else 
                Rng.Offset(0, xOffsetColumn).ClearContents 
            End If 
        Next 
        Application.EnableEvents = True 
    End If 
End Sub

Am using the following code that works for populating static dates in column C when data is filled in column B.

I would like to also have column E populated with static dates if data is filled in column D. Please advise tq

Private Sub Worksheet_Change(ByVal Target As Range) 
    'Update 20140722 
    Dim WorkRng As Range 
    Dim Rng As Range Dim xOffsetColumn As Integer 
    Set WorkRng = Intersect(Application.ActiveSheet.Range("B:B"), Target) 
    xOffsetColumn = 1 
    If Not WorkRng Is Nothing Then 
        Application.EnableEvents = False 
        For Each Rng In WorkRng 
            If Not VBA.IsEmpty(Rng.Value) Then 
                 Rng.Offset(0, xOffsetColumn).Value = Now 
                 Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss" 
            Else 
                Rng.Offset(0, xOffsetColumn).ClearContents 
            End If 
        Next 
        Application.EnableEvents = True 
    End If 
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一花一树开 2025-01-18 13:01:05

工作表更改:时间戳和清除

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const fRow As Long = 1 ' possibly 2 or more to exclude headers
    Const cOffset As Long = 1
    
    ' Reference the intersecting range.
    Dim irg As Range ' Intersect
    With Columns("B")
        With .Resize(.Rows.Count - fRow + 1).Offset(fRow - 1)
            'Debug.Print Union(.Cells, .EntireRow.Columns("D")).Address
            Set irg = Intersect(Union(.Cells, .EntireRow.Columns("D")), Target)
        End With
    End With
    If irg Is Nothing Then Exit Sub
    
    Dim trg As Range ' Time
    Dim crg As Range ' Clear
    Dim iCell As Range
    
    ' Combine cells into the Time and Clear ranges.
    For Each iCell In irg.Cells
        If Not VBA.IsEmpty(iCell.Value) Then
            If trg Is Nothing Then Set trg = iCell _
                Else Set trg = Union(trg, iCell)
        Else
            If crg Is Nothing Then Set crg = iCell _
                Else Set crg = Union(crg, iCell)
        End If
    Next iCell
    
    Application.EnableEvents = False
    
    ' Write.
    If Not trg Is Nothing Then
        With trg.Offset(, cOffset)
            .NumberFormat = "dd-mm-yyyy, hh:mm:ss"
            .Value = Now
        End With
    End If
    If Not crg Is Nothing Then
        crg.Offset(, cOffset).ClearContents
    End If
   
    Application.EnableEvents = True

End Sub

A Worksheet Change: Time Stamp and Clear

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const fRow As Long = 1 ' possibly 2 or more to exclude headers
    Const cOffset As Long = 1
    
    ' Reference the intersecting range.
    Dim irg As Range ' Intersect
    With Columns("B")
        With .Resize(.Rows.Count - fRow + 1).Offset(fRow - 1)
            'Debug.Print Union(.Cells, .EntireRow.Columns("D")).Address
            Set irg = Intersect(Union(.Cells, .EntireRow.Columns("D")), Target)
        End With
    End With
    If irg Is Nothing Then Exit Sub
    
    Dim trg As Range ' Time
    Dim crg As Range ' Clear
    Dim iCell As Range
    
    ' Combine cells into the Time and Clear ranges.
    For Each iCell In irg.Cells
        If Not VBA.IsEmpty(iCell.Value) Then
            If trg Is Nothing Then Set trg = iCell _
                Else Set trg = Union(trg, iCell)
        Else
            If crg Is Nothing Then Set crg = iCell _
                Else Set crg = Union(crg, iCell)
        End If
    Next iCell
    
    Application.EnableEvents = False
    
    ' Write.
    If Not trg Is Nothing Then
        With trg.Offset(, cOffset)
            .NumberFormat = "dd-mm-yyyy, hh:mm:ss"
            .Value = Now
        End With
    End If
    If Not crg Is Nothing Then
        crg.Offset(, cOffset).ClearContents
    End If
   
    Application.EnableEvents = True

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文