1

I want to download historic data from NASDAQ using VBA. I have managed to change the "FromDate" to the proper range but I haven't managed to get the code to hit the "Search" button (as it has no ID) which extracts the data. Please help me out.

MY VBA:

Sub OMX_data()

Dim URL As String
Dim ie As InternetExplorer

Set ie = New InternetExplorer
ie.Visible = True

ie.navigate ("http://www.nasdaqomxnordic.com/indexes/historical_prices?Instrument=DK0060368991")

Do
DoEvents
Loop Until ie.readyState = 4

ie.document.all("FromDate").Value = "2018-01-01"

End Sub

THE HTML

<div class="filterAreaRow overflowHidden">
<div class="button doSearch floatRight ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" onclick="historical.load();return false;" role="button" aria-disabled="false"><span class="ui-button-text">Search</span></div>
</div>

1 Answer 1

1

You can get search button element by class using getElementsByClassName and can trig it by Click method, doSearch is the name of search button in this form.

The last two line is the solution of your question.

Sub OMX_data()
    Dim URL As String
    Dim ie As InternetExplorer

    Set ie = New InternetExplorer
    ie.Visible = True

    ie.navigate ("http://www.nasdaqomxnordic.com/indexes/historical_prices?Instrument=DK0060368991")

    Do
        DoEvents
    Loop Until ie.readyState = 4

    ie.Document.all("FromDate").Value = "2018-01-01"

    Set search_button = ie.Document.getElementsByClassName("doSearch")
    search_button(0).Click
End Sub

You can use HTML DOM to import data to Excel, Below is example to import data to Excel

Sub OMX_data()
    Dim URL As String
    Dim ie As InternetExplorer

    Set ie = New InternetExplorer
    ie.Visible = True

    ie.Navigate ("http://www.nasdaqomxnordic.com/indexes/historical_prices?Instrument=DK0060368991")

    Do
        DoEvents
    Loop Until ie.ReadyState = 4

    ie.Document.all("FromDate").Value = "2018-01-01"

    Set search_button = ie.Document.getElementsByClassName("doSearch")
    search_button(0).Click

    ' Wait for filter apply
    Application.Wait (Now + TimeValue("0:00:05"))

    ' Use HTML DOM
    Set result = ie.Document.getElementById("historicalOutput").getElementsByTagName("tbody")(0).getElementsByTagName("tr")

    i = 0   ' for row
    For Each r In result
        j = 0   ' for column
        For Each c In r.Children
            Range("A1").Offset(i, j).Value = c.innertext
            j = j + 1
            DoEvents
        Next
        i = i + 1
    Next

    MsgBox "Import finish"
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic! It works. Can you also help with me getting the data to excel or should that be in a different question? <table id="historicalTable" class="tablesorter tablesorter-default" border="0" cellpadding="0" cellspacing="0" role="grid">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.