如何通过Application.Ontime同时运行两个Sub?

时间:2012-03-02 04:39:59

标签: excel vba user-defined-functions subroutine ontime

我有一个子如下:

Function SumOf(Byval a As Integer, Byval b As Integer)
  Call CalHelper.Sum(a,b)
  Call CalHelper.Sum(a+1,b+1)
  SumOf = "OK"
End Function

CalHelper模块包含:

'BEGIN Declare 
Private mWindowsTimerID As Long
Private mApplicationTimerTime As Date

Private numberA As Variant
Private numberB As String
'END Decalare

'Register `user32` lib's functions BEGIN
Public Declare Function SetTimer Lib "user32" ( _
      ByVal HWnd As Long, _
      ByVal nIDEvent As Long, _
      ByVal uElapse As Long, _
      ByVal lpTimerFunc As Long _
   ) As Long

Public Declare Function KillTimer Lib "user32" ( _
      ByVal HWnd As Long, _
      ByVal nIDEvent As Long _
   ) As Long
'Register `user32` lib's functions BEGIN

Sub Sum(ByVal a As Integer, ByVal b As Integer)


    On Error Resume Next
    mCacheUrl = keyy
    mCacheValue = valuee
    On Error GoTo 0

    If mWindowsTimerIDCache <> 0 Then
        KillTimer 0&, mWindowsTimerICache
    End If

    mWindowsTimerID = SetTimer(0&, 0&, 1000, AddressOf AfterUDFRoutine1)

End Sub

Private Sub AfterUDFRoutine1()

    ' Stop the Windows timer
    On Error Resume Next
    KillTimer 0&, mWindowsTimerID
    On Error GoTo 0
    mWindowsTimerID= 0

    ' Cancel any previous OnTime timers
    If mApplicationTimerTimeCache <> 0 Then
       On Error Resume Next
       Application.OnTime mApplicationTimerTime, "AfterUDFRoutine2", , False
       On Error GoTo 0
    End If

    mApplicationTimerTime = Now
    Application.OnTime mApplicationTimerTime, "AfterUDFRoutine2"

End Sub

Private Sub AfterUDFRoutine2()
   ' Do tasks not allowed in a UDF...
   Application.ScreenUpdating = False
   Application.Calculation = xlCalculationManual

   sum =numberA + numberB
   roww = Sheets(AddSheetCacheHelper.SHEET_CACHE_NAME).Cells(Rows.Count, "A").End(xlUp).Row + 1
   Sheet("Result").Cells(roww,1).Value = sum

   Application.Calculation = xlCalculationAutomatic
   Application.ScreenUpdating = True
End Sub

我的问题在于SumOf函数,只运行一个调用和。我想因为2个调用sub同时发生,所以mApplicationTimerTime是相同的,Applicaition.OnTime unknown必须运行什么?

更新 SumOf是我的加载项中的一项功能。这不是一个宏。

0 个答案:

没有答案