Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

html - Excel VBA to activate selectitem in web and go web for data

I'm very fresh to excel VBA. I tried use all potential solutions in the internet for activate selectitem in web and get data table but it didn't work. My Excel VBA code and Javascript is below.

Sub GetQuarterFinancials()

Dim ie As Object, URL As String

    URL = "https://www.marketwatch.com/investing/stock/aapl/financials"
    Set ie = CreateObject("internetexplorer.application")

With ie
 .Visible = True
 .navigate URL

 Do While .Busy
   DoEvents
 Loop
 Do While .readyState <> 4
   DoEvents
 Loop

  Set ticker = ie.document.getElementById("autocomplete_input")
  ticker.value = "FB"

  .document.getElementById("investing_ac_button").Click
  Application.Wait (Now + TimeValue("00:00:05"))

  Set selectitem = ie.document.getElementsByTagName("select")
  For Each i In selectitem
           i.value = "/investing/stock/msci/financials/income/quarter"
           i.FireEvent ("onchange")
      Next i
 End With
End Sub





    <select style="float:right" onchange="window.location = this.options[selectedIndex].value;">
        <option value="/investing/stock/msci/financials" selected="selected">Annual Financials</option>
        <option value="/investing/stock/msci/financials/income/quarter" >Quarter Financials</option>
    </select>
    <div style="clear:both"></div>
</div>
    <div class="block">
        <h2>Annual Financials for MSCI Inc.</h2>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use a css attribute = value selector to target the appropriate option from dropdown then select it.

Option Explicit

'VBE > Tools > References:
' Microsoft Internet Controls

Public Sub MakeSelections()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "https://www.marketwatch.com/investing/stock/aapl/financials"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .querySelector("#autocomplete_input").Value = "FB" 'you could avoid search by adding ticker into url
            .querySelector("#investing_ac_button").Click
        End With

        While .Busy Or .readyState < 4: DoEvents: Wend

        Dim ele As Object
        With .document
            Do
            On Error GoTo 0
            Set ele = .querySelector("[value^='/investing/stock/fb/financials/income/quarter']")
            On Error Resume Next
            Loop While ele Is Nothing
            .querySelector("[value^='/investing/stock/fb/financials/income/quarter']").Selected = True
            .querySelector(".financials select").FireEvent "onchange"
                Stop 'delete me later
        End With
        .Quit
    End With
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...