Hello All,
NSE has recently change the method of retrieving historical data from "GET" method to "POST" method. Hence, the historical data can no longer be downloaded by sending scrip code and date in the link. Based on various programs, I have tried to develop a VBA code which worked for me.
The VBA code below can be automated to suit once need.
Regards,
Amod
----------------------------------------
Public Sub downloadData()
'Open an excel sheet and rename one of the sheets as "DailyData". The data will be
'copied to that sheet
Dim frmDate As String, toDate As String, scrip As String
frmDate = DateSerial(2017, 5, 22)
frmDate = Format(frmDate, "dd-mm-yyyy")
toDate = DateSerial(2017, 5, 26)
toDate = Format(toDate, "dd-mm-yyyy")
scrip = "LUPIN"
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("DailyData")
'Call subroutine to download the data
getNSE_post frmDate, toDate, 1, 1, scrip, ws
End Sub
Sub getNSE_post(frmDate As String, toDate As String, nRow As Integer, _
nCol As Integer, scrip As String, ws As Worksheet)
Dim ie As Object
Dim frm As Variant
Dim element, submitInput As Variant
Dim rowCollection, htmlRow As Variant
Dim rowSubContent, rowSubData As Variant
Dim i, j, k, pauseTime As Integer
Dim anchorRange As Range, cellRng As Range
Dim start
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "
https://www.nseindia.com/products/content/equities/equities/eq_security.htm"
While ie.readyState <> 4: DoEvents: Wend
'try to get form by ID
Set frm = ie.document.getElementById("histForm")
ie.Visible = True
ie.document.getElementById("dataType").Value = "priceVolumeDeliverable"
ie.document.getElementById("symbol").Value = scrip
ie.document.getElementById("segmentLink").Value = 3
ie.document.getElementById("symbolCount").Value = 1
ie.document.getElementById("series").Value = "EQ"
'ie.document.getElementById("dateRange").Value = "day"
ie.document.getElementById("rdPeriod").Checked = True
ie.document.getElementById("fromDate").Value = frmDate
ie.document.getElementById("toDate").Value = toDate
'Pause For User To See Entry
pauseTime = 2 ' Set duration in seconds
start = Timer ' Set start time.
Do While Timer < start + pauseTime
DoEvents ' Yield to other processes.
Loop
For Each submitInput In ie.document.getElementsByTagName("INPUT")
If InStr(submitInput.getAttribute("onclick"), "submitData") Then
submitInput.Click
Exit For
End If
Next
Set anchorRange = ws.Cells(1, 1)
i = -1 'The header row needs to be omitted
j = 0
Set rowCollection = ie.document.getElementsByTagName("tr")
For Each htmlRow In rowCollection
Set rowSubContent = htmlRow.getElementsByTagName("td")
k = 0
For Each rowSubData In rowSubContent
If j = 2 Or j = 8 Or j = 14 Then
anchorRange.Offset(i, k).Value = rowSubData.innerText
k = k + 1
ElseIf j >= 4 And j <= 6 Then
anchorRange.Offset(i, k).Value = rowSubData.innerText
k = k + 1
ElseIf j = 10 Then
anchorRange.Offset(i, k).Value = rowSubData.innerText
anchorRange.Offset(i, k).Value = Replace(anchorRange.Offset(i, k).Value, ",", "")
k = k + 1
End If
j = j + 1
Next rowSubData
j = 0
i = i + 1
Next htmlRow
Exit Sub
errHandler:
Application.Calculation = xlCalculationAutomatic
MsgBox Err.Description, vbExclamation, "Download Error: the remote server did not respond!"
Err.Clear
Exit Sub
End Sub