工作簿、保存、关闭、重新打开(a/时间值)、重复

Workbook, Save, Close, Re-Open (w/ TimeValue),Repeat

宏 运行 带有分配给 "CloseMe" 的按钮。它曾经 运行 满足我的需要,但现在不再需要了(因为我尝试在另一个工作簿中使用此代码但没有成功)。现在它保存、关闭、等待 10 秒重新打开,但随后立即关闭。

Sub CloseMe()
Application.OnTime Now + TimeValue("00:00:10"), "OpenMe"
ThisWorkbook.Close True
End Sub

Sub OpenMe()
Application.OnTime Now + TimeValue("00:10:00"), "OpenMe"
ThisWorkbook.Close True
End Sub

我需要代码保存,关闭,等待 10 秒重新打开,保持打开状态 10 分钟(收集实时数据),然后重复这个过程(直到我手动中断它停止)。谢谢

您正在为 Open 和 Close sub 调用 OpenMe sub。

如果您希望它自动 运行,除了命令按钮之外,关闭子在哪里被调用?

代码执行您要求它执行的操作:a。 CloseMe 安排 OpenMe 从现在开始 10 秒并关闭工作簿,然后 b。 Excel 重新打开工作簿并调用 OpenMe,它安排 自身 从现在开始持续 10 分钟,然后立即继续关闭工作簿,最后 Excel 10 分钟后在 b 处继续循环。

我的理解是您的代码必须在 OpenMeCloseMe 中执行某些操作,因此您不希望只安排一次通话并关闭工作簿。此外,为了循环,一个子需要安排另一个。从广义上讲,您可以遵循这些路线:

Sub CloseMe()
    'Here, do whatever (if anything) must be done just before saving the workbook.
    '...

    'Schedule the OpenMe execution in 10 seconds.
    'I don't understand why you need to close the workbook, but that's not the question.
    Application.OnTime Now + TimeValue("00:00:10"), "OpenMe"
    ThisWorkbook.Close True
End Sub

Sub OpenMe()
    'Here, do whatever (if anything) must be done just as the workbook opens.
    '...

    'Schedule the CloseMe execution in 10 minutes.
    Application.OnTime Now + TimeValue("00:10:00"), "CloseMe"
End Sub

@Excellosaurus 我们很亲近。感谢您在不同的潜艇上对此进行逻辑解释。这是完整的代码。它有效,但在录制、关闭和重新打开时,我的时间戳加倍了。我正在捕获一些 RTD,为了让 RTD 刷新,您需要打开和关闭工作簿。我尝试插入 ActiveWorkbook.ForceFullCalculation = True 以避免额外的 open/close 子,但 RTD 没有使用它重新计算,所以唯一的方法是 运行 一个 open/close 子。

Dim NextTime As Double
Sub RecordData()
Dim Interval As Double
Dim cel As Range, Capture As Range
Application.StatusBar = "Recording Started"
Set Capture = Worksheets("Dashboard").Range("C5:K5") 'Capture this row of data
With Worksheets("Journal") 'Record the data on this worksheet
Set cel = .Range("A2") 'First timestamp goes here
Set cel = .Cells(.Rows.Count, cel.Column).End(xlUp).Offset(1, 0)
cel.Value = Now
cel.Offset(0, 1).Resize(1, Capture.Cells.Count).Value = Capture.Value
End With
NextTime = Now + TimeValue("00:01:00")
Application.OnTime NextTime, "RecordData"
End Sub

Sub StopRecordingData()
Application.StatusBar = "Recording Stopped"
On Error Resume Next
Application.OnTime NextTime, "OpenMe", , False
On Error GoTo 0
End Sub

Sub OpenMe()
Call RecordData
Application.OnTime Now + TimeValue("00:10:00"), "CloseMe"
End Sub

Sub CloseMe()
Application.OnTime Now + TimeValue("00:00:10"), "OpenMe"
ThisWorkbook.Close True
End Sub