【问题标题】:Scheduled VBA task and 'Application.OnTime'计划的 VBA 任务和“Application.OnTime”
【发布时间】:2013-08-16 09:02:59
【问题描述】:

我有以下运行良好的VBA 代码。它正在调用另一个 VBA Sub 没有任何问题:

Public Sub AutoPrintMissingHistoric()
    Dim qdf As DAO.QueryDef
    Dim rcs As DAO.Recordset
    Dim db As DAO.Database
    Dim j As Integer
    Dim flag As Boolean
    Dim i As Long
    Dim value_start, value_end As String
    Dim tmp As Date
    Dim wbRiskedge As Workbook
    Dim wsAccueil As Worksheet
    Dim wsHistoric As Worksheet

    Set wbRiskedge = Workbooks(StrWbRiskedge)
    Set wsAccueil = wbRiskedge.Worksheets(StrWsAccueil)
    Set wsHistoric = wbRiskedge.Worksheets(StrWsHistoricMissing)
    If FistTime = True Then
        Call Initialisation.CleanTab
    Else
        FistTime = True
        Call Initialisation.Initialisation
    End If
    vDelay = 5
    Cpt = Cpt + 1
    Set db = DBEngine.OpenDatabase(strDB)
    Set qdf = db.QueryDefs("Get_missing_fixings")
    If Cpt <= wsAccueil.Range(ManualListLetter & "1").End(xlDown).Row Then
        Application.StatusBar = wsAccueil.Cells(Cpt, ManualListLetter).Text
        qdf.Parameters("arg1") = wsAccueil.Cells(Cpt, ManualListLetter).Value
        Set rcs = qdf.OpenRecordset
        j = 0
        i = 1
        flag = False
        If Not rcs.EOF Then
            rcs.MoveLast
            rcs.MoveFirst
            While Not rcs.EOF
                j = 0
                While j < rcs.Fields.Count
                    If flag = False Then
                        With Cells(i, j + 1)
                            If .Value = "" Then
                                .Value = rcs(j).Name
                                .Font.Bold = True
                                .HorizontalAlignment = xlCenter
                                .VerticalAlignment = xlBottom
                            End If
                        End With
                    Else
                        Cells(i, j + 1).Value = rcs(j).Value
                    End If
                    j = j + 1
                Wend
                If flag = False Then
                    flag = True
                End If
                i = i + 1
                rcs.MoveNext
            Wend
            Call ChangeMinMax(rcs.RecordCount, CellMinDate, CellMaxDate, wsHistoric)
            Call ParseParameters
            Call SetReutersFunction
        End If
        rcs.Close
        qdf.Close
        db.Close
        wsHistoric.Calculate
        Application.StatusBar = wsAccueil.Cells(Cpt, ManualListLetter).Text & " - Next Function: FindMissingValue.AutoFindMissingValue"
        sToCall = "FindMissingValue.AutoFindMissingValue"
        MTimeGT = Time + TimeValue("00:00:" & vDelay)
        Application.OnTime MTimeGT, sToCall
    End If
End Sub

我把这个过程的执行放在一个计划任务中。但显然我的代码没有很好地执行:FindMissingValue.AutoFindMissingValue Sub 没有被调用,因为 Excel 刚刚关闭。

我认为是因为Application.OnTime MTimeGT, sToCall...可能是什么原因?

这里有FindMissingValue.AutoFindMissingValue的代码

Sub AutoFindMissingValue()
    Dim wbRiskedge As Workbook
    Dim wsAccueil As Worksheet
    Dim wsHistoric As Worksheet
    Dim i, nbResult As Long

    Set wbRiskedge = Workbooks(StrWbRiskedge)
    Set wsAccueil = wbRiskedge.Worksheets(StrWsAccueil)
    Set wsHistoric = wbRiskedge.Worksheets(StrWsHistoricMissing)
    If Left(wsHistoric.Range(ReutersFormula).Text, 13) Like "Retrieving...*" = True Then
        sToCall = "FindMissingValue.AutoFindMissingValue"
        MTimeGT = Time + TimeValue("00:00:05")
        Application.OnTime MTimeGT, sToCall
        Exit Sub
    End If
    i = WorksheetFunction.CountA(Columns(DateColumn & ":" & DateColumn))
    If WorksheetFunction.CountA(Columns(ColumnResearchVResult & ":" & ColumnResearchVResult)) > 0 Then
        wsHistoric.Range(FirstCellResearchVResult & ":" & ColumnResearchVResult & WorksheetFunction.CountA(Columns(ColumnResearchVResult & ":" & ColumnResearchVResult))).ClearContents
    End If
    nbResult = wsHistoric.Range(FirstResult).End(xlDown).Row
    wsHistoric.Range(ColumnResearchVResult & LineResearchVResult - 1).Value = "Results"
    If WorksheetFunction.CountA(Columns(DateColumn & ":" & DateColumn)) > 1 Then
        wsHistoric.Range(FirstCellResearchVResult & ":" & ColumnResearchVResult & i).FormulaLocal = "=RECHERCHEV($" & DateColumn & "$" & LineResearchVResult & ":$" & DateColumn & "$" & i & ";" & FirstLockResult & ":$" & ValueResultColumn & "$" & nbResult & ";2;0)"
    End If
    Application.StatusBar = wsAccueil.Cells(Cpt, ManualListLetter).Text & " - Next Function: FindMissingValue.AutoPutResultInDb"
    sToCall = "FindMissingValue.AutoPutResultInDb"
    MTimeGT = Time + TimeValue("00:00:01")
    Application.OnTime MTimeGT, sToCall
End Sub

【问题讨论】:

  • 您是否尝试过调试单步调试代码?
  • 调试无法与计划任务一起工作...所以我使用了MsgBox 我在这个Sub 中放了一个,在Sub 中放了一个名为FindMissingValue.AutoFindMissingValueFindMissingValue.AutoFindMissingValue 中的那个永远不会被执行,而Application.OnTime MTimeGT, sToCall 之前的那个会被执行。所以我认为这是因为Application.OnTime 但我不知道如何解决它....

标签: vba excel scheduled-tasks


【解决方案1】:

Application.OnTime 部分是正确的,应该毫无问题地调用FindMissingValue.AutoFindMissingValue(5 秒后)。可能发生的情况是,在这 5 秒期间,代码继续运行,返回到调用 AutoPrintMissingHistoric 的位置,并且工作簿可能会在这 5 秒过去之前关闭(尽管根据您的确切条件,即使工作簿已关闭,也应调用函数)。

您可以减少等待时间(例如vDelay = 1)或直接调用函数(Call FindMissingValue.AutoFindMissingValue)。实际上,我不确定您为什么要依靠Application.OnTime 来调用该函数;使用它可以“启动进程”(例如,“我希望我的宏每天在 00:00 执行”),但如果定期使用,可能会导致“混乱情况”。

如果这些都不起作用,请提供FindMissingValue.AutoFindMissingValue的代码以查看它。

注意:经过进一步的测试/讨论,我可以确认OnTime 在这些特定条件下的行为“太不规则”。您应该想出一种不同的方法来允许您需要等待时间,或者在不得不依赖OnTime 的情况下,进行密集的反复试验以确保其行为完全受到控制。该函数预计会被调用一次(例如在特定时间打开电子表格),因此在不同的上下文中使用它时必须非常注意(例如:在函数中调用它)。

【讨论】:

  • 当我回复 Mehow 时,调试不适用于计划任务...所以我使用了 MsgBox 我在此 Sub 中放置了一个,在 Sub 中放置了一个名为 FindMissingValue.AutoFindMissingValueFindMissingValue.AutoFindMissingValue 中的那个永远不会被执行,而Application.OnTime MTimeGT, sToCall 之前的那个会被执行。所以我认为这是因为Application.OnTime 但我不知道如何解决它....
  • @Yumino 请阅读我的回答。将 vDelay 减少到其最小表达式 (1) 或直接调用该函数(在此上下文中使用 Application.OnTime 的确切意义是什么?)。否则,请提供 FindMissingValue.AutoFindMissingValue 的代码。您的代码中的 Application.OnTime 被正确调用,因此我们需要更多信息来了解为什么它在您的计算机上无法正常运行(或者,停止使用它,因为如前所述,我不确定这样做的确切意义) .
  • 我正在使用Application.OnTime,因为在我的工作表中,我正在使用一个工具来获取一些外部数据。如您所知,当您执行 VBA 代码时,您的 Excel 工作表被阻止。 Application.OnTime 是我发现在我的情况下确保工作表将在 5 秒内刷新的唯一方法。这足以让工具检索数据。所以在我的过程中wsHistoric.Calculate 将刷新工作表并启动该工具。 Application.OnTime MTimeGT, sToCall 将让工具执行他的过程,因为 Excel 工作表将由 VBA 发布。
  • 我可以给出另一个子的代码,但我确信他工作得很好。没有预定任务,我所有的潜艇都运行良好。但是对于计划任务,它会在这个函数结束时停止
  • @Yumino 我很高兴看到你终于解决了你的问题。 Mehow 很高兴为解决这个问题做出贡献。
猜你喜欢
  • 2010-12-21
  • 2013-02-05
  • 2021-04-19
  • 1970-01-01
  • 2023-03-11
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多