View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Charlotte E.[_3_] Charlotte E.[_3_] is offline
external usenet poster
 
Posts: 160
Default From where is my UDF called?

Thanks, GS - that was very inspirational - I'll definately take a closer
look at this technique :-)


CE


Den 10.05.2013 19:44, GS skrev:
I can elaborate a bit as to how I manage tracking the source of a call.
I use a CallerID variable that I assign each procedure's name to...

Sub MyProc()
Const sSource$ = "MyProc"
....other code
End Sub

Function MyFunc$()
Const sSource$ = "MyFunc()"
....other code
End Function

...where I differentiate between Subs/Functions by including the
parenthesis in the function's CallerID.

This serves severall purposes now but the original intent of
implementing this was for central error handling where an 'error.log'
file is used. More recently I've been using it to determine redirects in
context to user actions initiated through the UI. As a bonus
convenience, it also augmented my 'EnableFastCode' routine so sub
callers wouldn't interupt the current runmode...


At the top of a standard module:
Type udtAppModes
Events As Boolean
CalcMode As Long
Display As Boolean
CallerID As String
End Type
Public AppMode As udtAppModes


Sub EnableFastCode(Caller$, Optional SetFast As Boolean = True)
'The following will make sure only the Caller has control,
'and allows any Caller to take control when not in use.
If AppMode.CallerID < Caller Then _
If AppMode.CallerID < "" Then Exit Sub

With Application
If SetFast Then
AppMode.Display = .ScreenUpdating: .ScreenUpdating = False
AppMode.CalcMode = .Calculation: .Calculation = xlCalculationManual
AppMode.Events = .EnableEvents: .EnableEvents = False
AppMode.CallerID = Caller
Else
.ScreenUpdating = AppMode.Display
.Calculation = AppMode.CalcMode
.EnableEvents = AppMode.Events
AppMode.CallerID = ""
End If
End With
End Sub

...where it is called whenever I need improved performance as follows...

EnableFastCode sSource '//turn it on
...do lots of stuff
EnableFastCode sSource, False '//turn it off

...which ensures another process will not disrupt the current state as
the first caller owns the process until done with it, then releases it
so it's available to be owned again by any caller that uses it.
Reasoning is that calls to other processes that also use this may be
initiated by the original caller, or another process called further in
the stack.