1

宏使用分配给“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 分钟(收集实时数据),然后重复这个过程(直到我手动中断它停止)。谢谢

4

3 回答 3

1

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

我的理解是您的代码必须在OpenMeor中执行某些CloseMe操作,因此您不想只安排通话并关闭工作簿。此外,要循环,一个潜艇需要安排另一个。从广义上讲,你可以沿着这些路线走:

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
于 2019-01-22T02:32:37.060 回答
0

您正在为 Open 和 Close 子程序调用 OpenMe 子程序。

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

于 2019-01-20T03:09:20.073 回答
0

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

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
于 2019-01-22T03:56:27.313 回答