【问题标题】:Userform Timestamp Cell When textbox Update文本框更新时的用户窗体时间戳单元格
【发布时间】:2023-03-06 22:27:01
【问题描述】:

我有一个用户表单,其中有两个文本框 txtCurrentStatustxtDCPreviousStatus

我希望 txtCurrentStatus 中的信息仅在自上次更新 txtCurrentStatus 后 15 天或更长时间后才显示在 txtDCPreviousStatus 中。

我有以下代码:

Sub txtDCCurrentStatus_AfterUpdate() 
    dim oldDate as Date
    dim timeStamp as String
    dim numOfDaysSinceLastUpdated as Integer

    'currentRow is the current row that is being updated from another part of my code
    oldDate = CDate(Cells(currentRow, 219).Value)
    timestamp = "Last updated on " & oldDate & Chr(13) & Chr(13)
    numOfDaysSinceLastUpdated = DateDiff("d", oldDate, Date) 'Date is today 
    If (numOfDaysSinceLastUpdated > 15) Then
        Me.txtDCPreviousStatus.Value = timestamp & Me.txtDCPreviousStatus.Value
        Me.txtDCPreviousStatus.Value = Me.txtDCCurrentStatus.Value & _
        Chr(13) & Me.txtDCPreviousStatus.Value
        Else
            'Do nothing
    End If
    Cells(currentRow, 219).Value = Format(Now, "mm-dd-yyyy")
End Sub

我有两个问题:

1) 如果 Cells(currentRow, 219).Value = ""because txtDCCurrentStatus 从未更新,那么我会收到一条消息

运行时错误“6”:溢出

numOfDaysSinceLastUpdated = DateDiff("d", oldDate, Date)

2) 为了解决这个问题,我将Cells(currentRow, 219).Value = Format(Now, "mm-dd-yyyy") 移到了最顶部。当我这样做时,文本永远不会移动到txtDCPreviousStatus,因为日期会在我做出更改的那一刻自动更改为今天,所以它永远不会超过 15 天。

基本上,我是:

  • 输入oldDate,最后更新日期txtDCCurrentStatus

  • 检查自 txtDCCurrentStatus 更新后是否已过去 15 天,方法是执行 oldDate - Date 然后将其放入 numOfDaysSinceLastUpdated

  • 如果是,我将txtDCCurrentStatus的内容移动到txtDCPreviousStatus
  • 现在保存txtDCCurrentStatus 更新为Cells(currentRow, 219).Value = Format(Now, "mm-dd-yyyy") 的新日期

有没有办法解决这两个问题?

【问题讨论】:

  • 问题是如果文本从未更新过,你想要什么?如何处理?
  • 是的,我的问题是知道如何处理以前从未更新过的文本
  • 如果没有oldDate 那么txtDCPreviousStatus 应该留空吗?
  • @ScottHoltzman 这太简单了,我几乎觉得很傻哈哈谢谢。我在你的建议中添加了一个 if 语句,它起作用了。没有更多溢出错误哈哈谢谢你:)
  • 那么,我猜你知道如何编码?

标签: excel timestamp userform vba


【解决方案1】:

感谢@ScottHoltzman 的回答。我解决了我的问题

我在Sub 的开头添加了以下内容,它起作用了

if oldDate = Empty then 
   Me.txtDCPreviousStatus.Value = ""
Else ...

我遇到的问题是,起初我检查的是if oldDate = ""。即使 oldDate 为空,oldDate 也是Date,当它为空时,它打印为"12:00:00 AM""" <> "12:00:00 AM" 所以我必须检查它是否是empty

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多