STOCKHISTORY #BUSY until VBA finishes

%3CLINGO-SUB%20id%3D%22lingo-sub-2134101%22%20slang%3D%22en-US%22%3ESTOCKHISTORY%20%23BUSY%20until%20VBA%20finishes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2134101%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20set%20the%20formula2%20property%20of%20a%20cell%20to%20STOCKHISTORY(...%20in%20a%20VBA%20macro.%26nbsp%3B%20Unfortunately%2C%20the%20cell%20value%20is%20%23BUSY%20until%20after%20my%20macro%20ends.%26nbsp%3B%20I%20have%20tried%20using%20delays%20and%20DoEvents.%26nbsp%3B%20Neither%20seems%20to%20help.%26nbsp%3B%20It%20looks%20like%20Excel%20waits%20until%20the%20macro%20is%20finished%20until%20it%20evaluates%20the%20function.%26nbsp%3B%20Is%20there%20anything%20I%20can%20do%20to%20make%20it%20evaluate%20it%20while%20macro%20is%20running%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2134101%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2134241%22%20slang%3D%22en-US%22%3ERe%3A%20STOCKHISTORY%20%23BUSY%20until%20VBA%20finishes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2134241%22%20slang%3D%22en-US%22%3E%3CP%3EI%20can%20only%20make%20guesses%2C%20because%20my%20version%20of%20Excel%20does%20not%20support%20STOCKHISTORY.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20suspect%20that%20Application.OnTime%20will%20work.%20(Note%3A%20__Not__%20Application.Wait.)%3CBR%20%2F%3E%3CBR%20%2F%3EHowever%2C%20that%20effectively%20suspends%20your%20macro.%20It%20does%20__not__%20permit%20concurrent%20Excel%20and%20VBA%20operations.%20Nothing%20does%2C%20AFAIK.%20Excel%20does%20not%20permit%20it%2C%20by%20design%20(with%20some%20exceptions).%3CBR%20%2F%3E%3CBR%20%2F%3EAlso%2C%20it%20might%20be%20difficult%20to%20redesign%20your%20VBA%20procedure%20so%20that%20it%20can%20be%20split%20into%20two%20procedures%2C%20the%20second%20one%20being%20the%20%22continuation%22%20when%20the%20OnTime%20event%20occurs.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20suspect%20that%20Application.OnTime%20Now%2BTimeSerial(0%2C0%2C1)%20will%20suffice%20for%20your%20purposes.%3CBR%20%2F%3E%3CBR%20%2F%3EBut%20FYI%2C%20Now%2BTimeSerial(0%2C0%2C1)%20does%20not%20always%20wait%20a%20full%201%20sec%20before%20triggering%20the%20event.%20On%20average%2C%20it%20waits%201%2F2%20second%3B%20but%20it%20might%20be%20less%20than%201%2F64%20second.%20The%20reason%20is%3A%20VBA%20Now%20rounds%20time%20down%20to%20the%20second%3B%20and%20system%20time%20is%20usually%20updated%20every%201%2F64%20second.%20(But%20some%20applications%20change%20that%20frequency.)%3CBR%20%2F%3E%3CBR%20%2F%3ENevertheless%2C%20I%20suspect%20that%20once%20the%20Excel%20thread%20starts%20the%20STOCKHISTORY%20operation%2C%20the%20OnTime%20event%20is%20deferred%20until%20at%20least%20STOCKHISTORY%20is%20done%2C%20if%20it%20does%20not%20execute%20in%20background.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2134606%22%20slang%3D%22en-US%22%3ERe%3A%20STOCKHISTORY%20%23BUSY%20until%20VBA%20finishes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2134606%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F960117%22%20target%3D%22_blank%22%3E%40axuary%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%22Doesn't%20work%22%20is%20a%20little%20inspecific.%26nbsp%3B%20But%20so%20was%20my%20first%20response.%26nbsp%3B%20I%20took%20a%20lot%20for%20granted.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%2C%20it%20might%20help%20if%20you%20attached%20an%20Excel%20file%20that%20demonstrates%20the%20problem.%26nbsp%3B%20If%20you%20can%20take%20the%20time%2C%20scale%20the%20workbook%20down%20to%20just%20the%20STOCKHISTORY%20formula(s).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESomething%20like%20the%20attached%20Excel%20file.%26nbsp%3B%20In%20VBA%20%22module4%22%2C%20I%20simulate%20the%20STOCKHISTORY%20delay.%26nbsp%3B%20Execute%20VBA%20procedure%20%22startit%22.%26nbsp%3B%20Procedure%20%22checkit%22%20checks%20A1%20(the%20simulated%20STOCKHISTORY%20formula)%20for%20a%20non-%23BUSY%20result%2C%20up%20to%204%20seconds.%26nbsp%3B%20Procedure%20%22mystockhistory%22%20runs%20in%202%20to%203%20seconds.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENote%3A%20I%20use%20%23N%2FA%20to%20simulate%20the%20%23BUSY%20error%2C%20which%20my%20version%20of%20Excel%20does%20not%20support.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20also%20attach%20the%20VBA%20procedures%20in%20an%20MSWord%20file%2C%20for%20the%20faint%20at%20heart.%20(wink)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDoes%20this%20help%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

I have set the formula2 property of a cell to STOCKHISTORY(... in a VBA macro.  Unfortunately, the cell value is #BUSY until after my macro ends.  I have tried using delays and DoEvents.  Neither seems to help.  It looks like Excel waits until the macro is finished until it evaluates the function.  Is there anything I can do to make it evaluate it while macro is running?

3 Replies

I can only make guesses, because my version of Excel does not support STOCKHISTORY.

I suspect that Application.OnTime will work. (Note: __Not__ Application.Wait.)

However, that effectively suspends your macro. It does __not__ permit concurrent Excel and VBA operations. Nothing does, AFAIK. Excel does not permit it, by design (with some exceptions).

Also, it might be difficult to redesign your VBA procedure so that it can be split into two procedures, the second one being the "continuation" when the OnTime event occurs.

I suspect that Application.OnTime Now+TimeSerial(0,0,1) will suffice for your purposes.

But FYI, Now+TimeSerial(0,0,1) does not always wait a full 1 sec before triggering the event. On average, it waits 1/2 second; but it might be less than 1/64 second. The reason is: VBA Now rounds time down to the second; and system time is usually updated every 1/64 second. (But some applications change that frequency.)

Nevertheless, I suspect that once the Excel thread starts the STOCKHISTORY operation, the OnTime event is deferred until at least STOCKHISTORY is done, if it does not execute in background.

@Joe User  Thanks for the response.  Unfortunately, that doesn't seem to work.

 

@axuary 

 

"Doesn't work" is a little unspecific.  But so was my first response.  I took a lot for granted.

 

First, it might help if you attached an Excel file that demonstrates the problem.  If you can take the time, scale the workbook down to just the STOCKHISTORY formula(s).

 

Something like the attached Excel file.  In VBA "module4", I simulate the STOCKHISTORY delay.  Execute VBA procedure "startit".  Procedure "checkit" periodically checks A1 (the simulated STOCKHISTORY formula) for a non-#BUSY result, up to 4 seconds.  You can adjust the frequency; I chose every 1/10 second.  Procedure "mystockhistory" runs in 2 to 3 seconds, simulating the completion of the STOCKHISTORY function.

 

Note: I use #N/A to simulate the #BUSY error, which my version of Excel does not support.

 

I also attach the VBA procedures in an MSWord file, for the faint at heart. (wink)

 

Does this help?