VBA macro works fine when stepping through (F8), but won't run correctly(F5)

ataylor135

New Member
Joined
Jul 17, 2016
Messages
3
Hello,

I'm trying to create a macro that will navigate to a certain page on a website. In order to do this, I have to navigate to the webpage, then click two buttons to get to where I need to be. My code works fine when I step through line by line, but when I try to run the code straight through, it seems to skip clicking the second button.
Here is my code:
Sub Navigate_To_Starting_Point()

The_Start:

Dim objIE As Object
Dim TB13 As MSForms.TextBox

Set objIE = CreateObject("InternetExplorer.Application")

objIE.Top = 0
objIE.Left = 0
objIE.Width = 800
objIE.Height = 600
objIE.Visible = True

objIE.Navigate ("http://openaccess.sb-court.org/")

Do
DoEvents

If Err.Number <> 0 Then

objIE.Quit
Set objIE = Nothing
GoTo The_Start:

End If

Loop Until objIE.ReadyState = 4

Set TB13 = UserForm1.TextBox13

TB13.Text = objIE.Document.Body.innerHTML


Dim the_input_elements1 As Object
Dim input_element1 As Object

Set the_input_elements1 = objIE.Document.getElementsByTagName("input")
For Each input_element1 In the_input_elements1
If input_element1.getAttribute("value") = "Civil, Fam law, Sm Cl, Probate" Then
input_element1.Click
Exit For
End If
Next input_element1

Do
DoEvents

If Err.Number <> 0 Then

objIE.Quit
Set objIE = Nothing
GoTo The_Start:

End If

Loop Until objIE.ReadyState = 4



Dim the_input_elements2 As Object
Dim input_element2 As Object


Set the_input_elements2 = objIE.Document.getElementsByTagName("input")

For Each input_element2 In the_input_elements2
If input_element2.getAttribute("value") = "Case Number Search" Then

input_element2.Click

Exit For
End If
Next input_element2

Do
DoEvents

If Err.Number <> 0 Then

objIE.Quit
Set objIE = Nothing
GoTo The_Start:

End If

Loop Until objIE.ReadyState = 4

End Sub

Any help you can provide will be greatly appreciated.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I googled the issue and certain post mentioned that the debugger runs the code differently than when you run the code straight through. What do they mean by this?
 
Upvote 0
My code works fine when I step through line by line, but when I try to run the code straight through, it seems to skip clicking the second button.
Stepping line by line introduces an artificial delay which allows the page to finish loading before the code accesses the HTML elements. Therefore the code needs to replicate this delay by waiting for the IE page to finish loading:
Code:
    With objIE
        While .Busy Or .readyState <> 4: DoEvents: Wend
        While .document.readyState <> "complete": DoEvents: Wend   'might not need this
    End With
I can't really follow your code because of the lack of indentation and because of the GoTo and Exit For statements.

Please use CODE tags when posting VBA code - the # icon in the message editor.
 
Upvote 0

Forum statistics

Threads
1,216,773
Messages
6,132,622
Members
449,740
Latest member
tinkdrummer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top