NSE印度报价历史下载自动化

时间:2017-08-31 08:43:37

标签: excel vba excel-vba internet-explorer

首先,我不是程序员,我只想通过在Excel中输入一些输入数据来从https://www.nseindia.com/products/content/equities/equities/eq_security.htm下载引用历史记录。我以某种方式设法将数据放入VBA。谁能帮助我如何点击"以CSV格式下载文件" &安培;将其保存到我的本地磁盘。

这是我的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

1 个答案:

答案 0 :(得分: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.将单元格引用更改为您的引用