0

When i go to fatch data using (from date) to (to date), click on show button using vba not working

Also i need suggestion, How can i download csv file from multiple dates

i wanted to download csv file for each date between date range

Dim MP, FD, TD As String
MP = ActiveWorkbook.Name
FD = Sheets("Sheet1").Range("XFA2")
TD = Sheets("Sheet1").Range("XFB2")
Dim IE As New SHDocVw.internetexplorer
Dim HTMLDoc As MSHTML.HTMLDocument
IE.Visible = True
IE.navigate "https://www.mcxindia.com/market-data/bhavcopy"
ShowWindow IE.hwnd, SW_SHOWMAXIMIZED
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
For N = FD To TD
Workbooks(MP).Worksheets("Sheet1").Range("XEX2").Value = (FD)
N1 = Workbooks(MP).Worksheets("Sheet1").Range("XEX3").Value
HTMLDoc.getElementById("txtDate").Value = (N1)
HTMLDoc.getElementById("btnShowDatewise").Click
Next N

Suggest me the codes to click on submit button and how to download multiple date files,since show button takes time to publish data and then i could click on csv tab on rightside penal of webpage

5
  • Please can you show the expected header row for your output. Commented Feb 19, 2019 at 5:50
  • @QHarr Expected Header Row "Date|Instrument|Commodity|Expiry Date|Option Type|Strike Price|Open|High|Low|Close|PCP|Volume (Lots)|Volume (000's)|Value (Lacs)|OI(Lots)" It should be in separate column Commented Feb 19, 2019 at 6:10
  • And where do you see the option to set a start date? Commented Feb 19, 2019 at 6:52
  • @QHarr i am inserting one date from date range and download file Commented Feb 19, 2019 at 6:57
  • So you are only selecting the one date yes? Commented Feb 19, 2019 at 7:00

1 Answer 1

1

If you are only setting the one day you can do the following which includes a check that the date produces a download file

Option Explicit   
Public Sub Download()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "https://www.mcxindia.com/market-data/bhavcopy"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .parentWindow.execScript "document.querySelector('#cph_InnerContainerRight_C001_txtDate_hid_val').value='20190201';"
            .parentWindow.execScript "document.querySelector('#txtDate').value='20190201';"
            .querySelector("#btnShowDatewise").Click

            If .querySelectorAll("#cph_InnerContainerRight_C001_lnkExpToCSV").Length > 0 Then
                .parentWindow.execScript "__doPostBack('ctl00$cph_InnerContainerRight$C001$lnkExpToCSV','');"
            End If

        End With

        While .Busy Or .readyState < 4: DoEvents: Wend
        '.Quit
    End With
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

is there any way to save download file on specific path?
is there any way to save downloaded file on specific path?
use selenium. If you can find a way to use xhr then you can also do urlmon/binary download

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.