gpt4 book ai didi

vba - Excel VBA 在用户窗体或 Excel 关闭时保持用户窗体计时器运行

转载 作者:行者123 更新时间:2023-12-03 02:32:23 28 4
gpt4 key购买 nike

最近我设法在用户窗体上找到一些有关计时器的代码,我的问题是即使用户窗体或 Excel 文件关闭,我也需要保持计时器运行...有人可以看一下代码吗并提供一些反馈?我的用户表单是:optionsForm

Dim dteStart As Date, dteFinish As Date  
Dim dteStopped As Date, dteElapsed As Date
Dim boolStopPressed As Boolean, boolResetPressed As Boolean

Private Sub Reset_Timer_Click()
dteStopped = 0
dteStart = 0
dteElapsed = 0
Tech_Timer = "00:00:00"
boolResetPressed = True
End Sub

Private Sub Start_Timer_Click()
Start_Timer:
dteStart = Time
boolStopPressed = False
boolResetPressed = False
Timer_Loop:
DoEvents
dteFinish = Time
dteElapsed = dteFinish - dteStart + dteStopped
If Not boolStopPressed = True Then
Tech_Timer = dteElapsed
If boolResetPressed = True Then GoTo Start_Timer
GoTo Timer_Loop
Else
Exit Sub
End If
End Sub

Private Sub Stop_Timer_Click()
boolStopPressed = True
dteStopped = dteElapsed
End Sub

Private Sub optionsForm_Initialize()
Tech_Timer = "00:00:00"
End Sub

最佳答案

计时器的想法不是让它运行,而是它记住一个时间点,并且可以给你这个时间点和当前时刻之间的差异。如果你每秒询问这个差异,那么它看起来就像 watch 一样在运行。

这样的事情将是一个好的开始。在xl_main中写入以下内容:

Option Explicit

Dim dtime As Date

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cells(1, 1).Value = dtime
End Sub

Private Sub Workbook_Open()

If Cells(1, 1).Value = 0 Then
dtime = Now
Else
dtime = CDate(Cells(1, 1))
End If

End Sub

您可以根据自己的意愿尝试并使其变得更好。例如。您可能会找到重置dtime或类似内容的方法。

关于vba - Excel VBA 在用户窗体或 Excel 关闭时保持用户窗体计时器运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42574783/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com