2017-08-31 47 views
0

首先,我不是程序員,我只想從https://www.nseindia.com/products/content/equities/equities/eq_security.htm下載報價歷史記錄,方法是在Excel中輸入一些輸入數據。我以某種方式設法將數據放入VBA中。任何人都可以請幫助我如何點擊「以CSV格式下載文件」&將其保存到我的本地磁盤。NSE India報價歷史記錄下載自動化

這裏是我的VBA代碼:

Private Sub CommandButton1_Click() 
    Dim IE As Object 
    With IE 
    Set IE = CreateObject("InternetExplorer.Application") 

'create new instance of IE. use reference to return current open IE if 
'you want to use open IE window. Easiest way I know of is via title bar. 
    IE.Navigate "https://www.nseindia.com/products/content/equities/equities/eq_security.htm" 
'go to web page listed inside quotes 
    IE.Visible = True 
    While IE.busy 
    DoEvents 'wait until IE is done loading page. 
    Wend 
    IE.document.ALL("symbol").Value = ThisWorkbook.Sheets("sheet1").Range("b1") 
    IE.document.ALL("series").Value = ThisWorkbook.Sheets("sheet1").Range("b2") 

    IE.document.getElementById("rdDateToDate").Click 

    IE.document.ALL("fromDate").Value = ThisWorkbook.Sheets("sheet1").Range("b4") 

    IE.document.ALL("toDate").Value = ThisWorkbook.Sheets("sheet1").Range("c4") 
    IE.document.getElementById("submitMe").Click 

    End With 

End Sub 
+0

你能提供的值單元格引用的'Symbol'和'Series' ?理想您的'從日期'和'迄今日期',但它們並不重要 – Zac

+0

符號= SBIN,系列=情商,從日期= 01-01-2012&到目前爲止2012年1月12日 –

+0

請提供代碼下載文件後會自動關閉IE。謝謝。 –

回答

0

下面UDF會做你在找什麼:

Private Sub CommandButton1_Click() 
    Dim IE As New InternetExplorer 
    Dim oW As Worksheet: Set oW = ThisWorkbook.Worksheets("Sheet4") 
    Dim oEleCol As MSHTML.IHTMLElementCollection 
    Dim oEle As MSHTML.IHTMLElement 


    With IE 
    'Set IE = CreateObject("InternetExplorer.Application") 

     ' Set IE 
     .Visible = True 
     'ShowWindow .hwnd, SW_SHOWMAXIMIZED 

     ' Navigate to URL 
     .Navigate "https://www.nseindia.com/products/content/equities/equities/eq_security.htm" 

     ' Wait for page to be ready 
     While .Busy 
      DoEvents 'wait until IE is done loading page. 
     Wend 

     ' Set criteria 
     .document.all("symbol").Value = oW.Range("I2") 
     .document.all("series").Value = oW.Range("I3") 
     .document.getElementById("rdDateToDate").Click 

     ' Wait for page to be ready 
     While .Busy 
      DoEvents 'wait until IE is done loading page. 
     Wend 

     ' Set remaining criteria 
     .document.all("fromDate").Value = oW.Range("I4") 
     .document.all("toDate").Value = oW.Range("I5") 

     ' Submit criteria 
     .document.getElementById("submitMe").Click 

     ' Wait for page to be ready 
     While .Busy 
      DoEvents 'wait until IE is done loading page. 
     Wend 

     ' Find the link to download file 
     Set oEleCol = .document.getElementsByTagName("A") 
     For Each oEle In oEleCol 
      If oEle.innerText = "Download file in csv format" Then 
       oEle.Click 
       Exit For 
      End If 
     Next 

     ' Wait for page to be ready 
     While .Busy 
      DoEvents 'wait until IE is done loading page. 
     Wend 

     ' Download file 
     DownloadFile .hwnd 

     ' Close IE 
     .Quit 

    End With 

End Sub 

注:
1.您可以註釋掉這行因爲我使用它來最大化瀏覽器窗口:ShowWindow .hwnd, SW_SHOWMAXIMIZED
2. DownloadFile是一個函數調用。您可以在這裏找到的功能:How to download a file from internet explorer using VBA
3.更改工作表名稱來無論你的表是
4.切換到任何你引用

+0

哪一行會引發錯誤? – Zac

+0

我懷疑它是拋出錯誤的IE對象。這是因爲我在創建IE對象的地方使用了「Microsoft Internet Controls」。點擊'工具'菜單,然後點擊'參考',引用上述控制。這將打開一個窗口。向下滾動,直到找到「Microsoft Internet Controls」,選擇它,然後按下「確定」按鈕。這應該可以解決問題 – Zac

+0

我更新了代碼,以便您不會再發生這些錯誤。不要忘記從我提供的鏈接中獲得** DownloadFile **功能 – Zac