View Full Version : VBA run-time error '91': Sometimes, Sometimes Not

06-06-2009, 12:58 AM
I have a VBA program, which at one point does early binding to Windows Explorer. I'm able to use VBA to automatically bring up a URL and then automatcially have my logon and password entered. Next, my program looks at certain cells in Excel and based on the data makes a decsion as to which radio buttons to click on in the Windows Explorer page (it's a page on my work's intranet). My program then automatically clicks the next button and the next page is brought up. Now up until this point I have no troubles. Next, based on info in excel my program is suppose to write the customer name in the first text box on the next Web page and also the customer ticket number in the second text box. But it is at this point (the customer name) I get the error: run-time error '91':
object variable or with block variable not set.

Now the strange thing is, if i step thru my program using the F8 key my program works fine. Or if i execute it once, then click "end" on the error 91 msg box and leave the webpage up and rerun my program it works fine too.

Now on my home computer my program always works fine, but not on my work computer, where i actually need it to work. I thought maybe my program was executing too fast (cause my computer is too fast) and was causing it to hang up, but i put a loop in my program to loop 100000 times right before i assign the customer name to the text box and i still get the same error. I just don't understand why if i step thru my program I don't get the "run time error 91" msg or if i run my program once and then press "end" on the error message, but don't close out the web page and rerun again i don't get the error msg.

Any info would be great.

06-06-2009, 01:25 AM
I added a msgbox right before my program gets the run time 91 error (right before my program tries to enter the customer name in the text box on the 2nd page), which stops my code and then when i press ok on the Msgbox my program works just fine. I then copied the loop I already had to slow my program down and pasted it two more times, so there are three loops in a row executing one after another, each one 100000 times and now my program works fine. So it looks like my computer was executing my code too fast and was getting hung up.

06-08-2009, 03:50 PM
Perhaps using Application.Wait (or Do Events) may be better than looping? I don't think the computer is ever too fast... it's just doing stuff in a different priority than you want it to. Real life example below for which I have no idea why the Wait is necessary. HTH. Dave

UserForm13.ListBox1.ListIndex = -1
Application.Wait (Now + TimeValue("0:00:01"))

02-29-2012, 05:37 PM

I just wanted to add some updated info to this thread because I had exactly the same problem (intermittent run-time error 91 when trying to control IE from VBA) in Feb 2012.

The detail of the problem, and the various solutions, is found at :

thread 617372 at mrexcel.com (apparently I can't post links in threads as a newbie ! Sheesh !)

three ws dot mrexcel dot com slash forum slash showthread dot php ?t=617372

Basically the OP was right- it *IS* speed-related... but it's the fact that IE has not finished building the DOM when you ask it to go find you the radio buttons (or other elements) in the loaded page...

So there are much more elegant solutions than just adding some delay loops and hoping that's enough time...

(I added this comment here because this thread came up in response to my google search and I thought it might help others with the same problem in the future...)

Warren. :-)

05-01-2018, 02:26 AM
Reinstall the Software/Application

Incomplete installation of a software/application may also result in “VBA runtime error 91.” Also, reinstallation of the software may also result in “VBA runtime error 91,” this is due to remnant files, DLL, and registry of the previously installed software. To fix this is to uninstall the software and run cleaner to delete remnant and temporary files.
Check this URL