2017-03-01 145 views
1

我想通过vba将“https://investing.com/indices/us-spx-500-historical-data”的月度数据转换为excel。将网络表格数据导入excel

这里是时间框架下拉菜单中的源代码: -

<select id="data_interval" class="newInput selectBox float_lang_base_1"> 
     <option value="Daily" selected="">Daily</option> 
     <option value="Weekly">Weekly</option> 
     <option value="Monthly">Monthly</option> 
    </select>enter code here 

这里是表源代码:

<table class="genTbl closedTbl historicalTbl" id="curr_table" tablesorter="">enter code here 

这里是我使用VBA代码,

Sub GetData() 
Dim IE As Object 
Dim Links 
Set IE = CreateObject("InternetExplorer.Application") 
IE.navigate ("https://uk.investing.com/indices/us-spx-500-historical-data") 
IE.Visible = True 
Do 
    DoEvents 
Loop Until IE.readyState = 4 
Set Links = IE.document.getElementById("data_interval") 
Links.selectedIndex = 2 
End Sub 

2是每月索引号,它可以从下拉菜单中选择月份,但表格不会从每周到每月,我也想将这些数据转化为excel

回答

0

尝试这样,它可能适用于您,取决于您的Internet Explorer。

Option Explicit 

Sub GetData() 

    Dim IE  As Object 
    Dim Links As Object 
    Dim evt  As Object 

    Set IE = CreateObject("InternetExplorer.Application") 
    IE.navigate ("https://uk.investing.com/indices/us-spx-500-historical-data") 
    IE.Visible = True 
    Do 
     DoEvents   
    Loop Until IE.readyState = 4 

    Set Links = IE.document.GetElementById("data_interval") 
    Set evt = IE.document.createevent("htmlevents") 
    evt.initevent "change", True, False 

    Links.Focus 
    Links.SelectedIndex = 2 
    Links.FireEvent ("onChange") 

    Links.dispatchevent evt 

End Sub 

悼念these guys为正确发射事件。

+0

感谢哥们,它的工作在以前的IE版本,但不是当前的IE版本。 –