【问题标题】:Copy just the values of date to another cell仅将日期的值复制到另一个单元格
【发布时间】:2021-12-31 03:20:21
【问题描述】:

我正在尝试根据以下内容复制当前日期和时间

if a cell in column E changes to Contacted, then need add the timestamp but it should not change when the next time i open the excel

我正在手动编辑E列,见下图

我知道要捕捉我使用 NOW() 的日期,但我的问题是第二天当我打开 Excel 工作表时,它会将日期更改为该日期。我不需要那个。

我希望日期只出现一次并且不要更改它。我怎样才能做到这一点?任何答案都非常感谢

我正在尝试下面的代码,但它不起作用

=IF((E2)="Contacted",NOW(),"")

【问题讨论】:

  • 我建议使用 via vba 并使用适当的 Worksheet 事件处理程序(使用 Worksheet_Change,似乎最适合您尝试做的事情)。其中,您可以简单地测试(例如)E2 变化并测试目标单元格是否为空。如果是,请写下日期。如果没有,就这样吧。
  • 能否详细说明基于excel中的一个事件?你得出的结论是NOW() 没用。 =IF((E2)="Contacted",NOW(),"") 是一个公式,而不是代码。您使用了 eventcode 这两个词,这可能是无意的,说它只能通过使用 VBA 而不是通过使用公式来完成。需要更多细节。通过添加它们来编辑您的问题。还是最后一个公式说:如果E 列中的单元格更改为Contacted,则添加时间戳?但不清楚在哪一列添加它。请务必澄清。
  • 确定已更新。 `如果E列中的单元格更改为已联系,则需要添加时间戳,但下次打开excel时不应更改`
  • @Spinner 你知道是否有办法不更改单元格中的更改日期,每天打开 Excel 表时。
  • @VBasic2008ok 添加了 vba 标签,我将 E 列手动更改为“已联系”

标签: excel vba datetime excel-formula


【解决方案1】:

对我上面的 cmets 的进一步说明:我的建议是完全放弃公式。

改为编写一个 vba Worksheet_Change 事件处理程序。
o vba 帮助中给出的示例向您展示了如何做到这一点。
o 此子项进入工作表的代码模块。

在事件处理程序中测试:

  1. 更改了感兴趣的单元格(即在 Columns("E") 中)
  2. 更改的单元格值是感兴趣的(即= "Contacted"
  3. 目标单元格(即时间戳所在的位置)为空(即= ""

如果满足条件:写入时间戳到目标单元格(即= Now()

注意:您可能还需要处理操作员错误(例如,更改为已联系,然后更改为其他内容)。
在这种情况下,您需要清除相关的时间戳单元格。

【讨论】:

    【解决方案2】:

    为下拉值添加时间戳

    • 将以下内容复制到工作表模块中,例如上述工作表的Sheet1(不是ThisWorkbookModule1)。
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        On Error GoTo ClearError
    
        Const sCriteria As String = "Contacted" ' Source Criteria
        Const sFirst As String = "E2" ' where the criteria will be searched for
        Const dCol As String = "F" ' where the time stamp will be added
        
        Dim scrg As Range ' Source Column Range
        With Me.Range(sFirst)
            Set scrg = .Resize(Me.Rows.Count - .Row + 1)
        End With
        Dim srg As Range: Set srg = Intersect(scrg, Target)
    
        If srg Is Nothing Then Exit Sub ' no change in the source column range
            
        Dim sCell As Range ' Source Cell
        Dim dwrg As Range ' Destination Write Range
        Dim dCell As Range ' Destination Cell
        
        ' Combine the cells to be written to into a range ('dwrg').
        For Each sCell In srg.Cells
            Set dCell = sCell.EntireRow.Columns(dCol)
            If IsEmpty(dCell) Then ' destination cell is empty
                If CStr(sCell.Value) = sCriteria Then ' is 'sCriteria'
                    If dwrg Is Nothing Then ' combine first cell
                        Set dwrg = dCell
                    Else ' combine any but the first cell
                        Set dwrg = Union(dwrg, dCell)
                    End If
                'Else ' is not 'sCriteria'
                End If
            'Else ' destination cell is not empty
            End If
        Next sCell
        
        ' Disable events to not retrigger this or any other while writing
        ' the timestamp(s).
        Application.EnableEvents = False
        
        ' Write the timestamp in one go.
        If Not dwrg Is Nothing Then ' at least one cell combined
            dwrg.Value = Now
        'Else ' no cells combined
        End If
    
    SafeExit:
        If Not Application.EnableEvents Then ' events disabled
            Application.EnableEvents = True
        'Else ' events enabled
        End If
        
        Exit Sub
    ClearError:
        Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
        Resume SafeExit
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2023-01-20
      • 2020-03-30
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 2018-03-15
      相关资源
      最近更新 更多