2017-08-31 20 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