【发布时间】:2023-03-06 22:27:01
【问题描述】:
我有一个用户表单,其中有两个文本框 txtCurrentStatus 和 txtDCPreviousStatus。
我希望 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