2

I have a sub as below:

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

And CalHelper Module contains:

'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

My problem is in SumOf function, just one call sum is run. I guess because 2 call sub are simultaneously happen, so mApplicationTimerTime is the same, and Applicaition.OnTime unknown must be run what?

Update SumOf is a function in my add-in. It's not a macro.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Davuz
  • 5,040
  • 13
  • 41
  • 61

0 Answers0