Strange / erratic behavior in CreatePivotTable ()
I am writing VBA code to automate some processes in Excel and I come across very strange behavior for which I could not find any documentation / help.
I have a procedure MAJ_GF
that first executes a function GF.Update
, checks the result, and then runs the procedure GF.Build
(which basically takes data received with help GF.Update
from different sheets and does a bunch of things with that).
At some point this "group of things" requires a pivot table, so it GF.Build
contains the following line:
Set pvt = ThisWorkbook.PivotCaches.Create(xlDatabase, _
"'source_GF'!R1C1:R" & j & "C" & k).CreatePivotTable("'TCD_GF'!R4C1", "GFTCD1")
Strange behavior:
- when i run
MAJ_GF
VBA executes correctlyGF.Update
, then startsGF.Build
and stops at the above line complaining about "Bad argument or procedure call" - when i start manually
GF.Update
then start manuallyGF.Build
everything goes smoothly andGF.Build
does what it is supposed to do from start to finish, no error - even stranger, when I set a breakpoint on the incriminated line, run
MAJ_GF
, then VBA pauses on the line as expected, and when I say Continue ... it just continues smoothly and without errors!
I've spun this around and around, double-checked the value of each variable and it just doesn't make sense.
Anyone ideas?
source to share
Few ideas come to my mind:
- An update is still taking place in the background. Try
DoEvents
andApplication.Wait
before the line you mentioned - Also check if any data connections can refresh in the background - if so turn off background refresh
- It is very rare (usually in the old version and when using charts) opening an Excel window (in case you used
Application.Visible = False
and enabledScreenUpdating
.. - Are you using any "exotic" links / add-ons? Disable them and check if the problem persists.
- Try restarting your computer.
Not that I'm too optimistic to solve your problem, but give it a try! Good luck!
source to share