Excel VBA site with dropdowns can't proceed

SKPork

New Member
Joined
Feb 11, 2021
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
  2. Web
I am new to VBA and I am able to get to a dropdown and select a value, but, I can't proceed from the value. It should open a subsequent dropdown with a little calendar. I will attach images of the script and the web page.

This is the code that I tried:

Option Explicit

Sub GetHogData()

Dim ie As New SHDocVw.InternetExplorer
Dim HTMLdoc As MSHTML.HTMLDocument
Dim HTMLTables As MSHTML.IHTMLElementCollection
Dim HTMLTable As MSHTML.IHTMLElement
Dim HTMLDiv As MSHTML.IHTMLElement
Dim TableSection As MSHTML.IHTMLElement
Dim TableRow As MSHTML.IHTMLElement
Dim TableCell As MSHTML.IHTMLElement
Dim RowText As String
Dim ieObj As InternetExplorer
Dim htmlEle As Object
Dim i As Integer
Dim List As Object
Dim url As String
Dim selectElement As HTMLSelectElement
Dim changeEvent As Object



ie.Visible = True
ie.navigate "MPR Data Mart"

Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
Loop

Set HTMLdoc = ie.Document
ie.Document.querySelector("[href*='Daily Swine']").Click
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
Loop
ie.Document.querySelector("[href*='(LM_HG201) National Daily Direct Hog Prior Day - Slaughtered Swine']").Click
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
Loop
ie.Document.getElementsByName("op_report_date")(0).Value = "GREATER"
Set changeEvent = HTMLdoc.createEvent("HTMLEvents")
DoEvents


End Sub
 

Attachments

  • USDA_Script.JPG
    USDA_Script.JPG
    94.6 KB · Views: 43
  • USDA_Page.JPG
    USDA_Page.JPG
    86.8 KB · Views: 43

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Welcome

VBA Code:
Sub GetHogData()
Dim ie As New SHDocVw.InternetExplorer, htmldoc As MSHTML.HTMLDocument
Dim htmlele As Object, i%, selectElement As HTMLSelectElement, changeEvent As Object
ie.Visible = True
ie.navigate "https://mpr.datamart.ams.usda.gov/menu.do?path=\Species\Hogs"
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
Loop
Set htmldoc = ie.Document
ie.Document.querySelector("[href*='Daily Swine']").Click
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
Loop
ie.Document.querySelector("[href*='(LM_HG201) National Daily Direct Hog Prior Day" & _
" - Slaughtered Swine']").Click
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
Loop
ie.Document.getElementsByName("op_report_date")(0).Value = "GREATER"
Set changeEvent = htmldoc.createEvent("HTMLEvents")
changeEvent.initEvent "change", True, False
ie.Document.getElementsByName("op_report_date")(0).dispatchEvent changeEvent
Set htmlele = ie.Document.getElementsByTagName("input")
For i = 1 To htmlele.Length
    If htmlele(i).getAttribute("name") = "report_date" Then
        htmlele(i).Value = "2/15/2021": Exit For
    End If
Next
clicker ie, "Continue*"
clicker ie, "Generate*"
clicker ie, "Export*"
End Sub

Sub clicker(ie As SHDocVw.InternetExplorer, op$)
Dim i%, htmldoc As MSHTML.HTMLDocument, htmlele As Object
Set htmldoc = ie.Document
Set htmlele = htmldoc.getElementsByTagName("input")
For i = 1 To htmlele.Length
    If htmlele(i).getAttribute("value") Like op Then
        htmlele(i).Click: Exit For
    End If
Next
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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