Just to be clear then:
UDF - retrieves data.
Your VBA code - updates Pivot Table.
What I don't understand is this - what kicks off your pivot table
update? I assume it is the VBA code that you have written that performs
this task (refresh) - if so *how do you trigger it*?. Are you using
events? This would appear to me the crux of the issue.
I'm assuming (again) that you are using the Worksheet_Calculate event to
trap the updating of your UDF cells. If this is the case I would propose
something like the following (placed in the respective worksheet module)
would do the trick:
Private Sub Worksheet_Calculate()
Dim p As PivotTable
Application.EnableEvents = False
Set p = Me.PivotTables(1)
p.RefreshTable
Set p = Nothing
Application.EnableEvents = True
End Sub
[Note - without the disabling events the above will fire once for every
cell, so they're pretty crucial here.]
This works on the example I set up for myself. I am understanding
correctly and if so.... does this work for you?
G
Program execution flow is almost same as described by you.
- User updates A1 manually.
- A2,B2,C2 etc get updated automatically (there are around 5000 cells); this
is an asynchronous operation.
- Hence the code execution continues and updates pivot tables whose inputs
are A2,B2, C2 etc. This happens before A2,B2 etc are updated by the
asynchronous UDF.
We tried including a msgbox statement just before the pivot table update
statement. Sometimes the OK button of the msgbox statement gets activated
only when the data retrieval is completed and the program works perfectly;
but this does not happen always.
Regards,
Venu
: