2017-03-01 183 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版本。 –