Daily Downloads using Macro

zaska

Well-known Member
Joined
Oct 24, 2010
Messages
1,046
Hi all,

I dowload zipped csv files daily from the following URL's . I would like to know whether the same can be done using VBA by selecting the Date in excel sheet.

http://www.bseindia.com/mktlive/market_summ/lonshrtpos.asp

Downloads this file from the above url ( http://www.bseindia.com/BSEDATA/gross/2011/SCBSEALL0706.zip)


http://www.bseindia.com/mktlive/bhavcopy.asp

Downloads this file from the above url (http://www.bseindia.com/bhavcopy/eq070611_csv.zip)

Kindly help me with the code for downloading the files.

Thank you in advance.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
hi all,

i dowload zipped csv files daily from the following url's . I would like to know whether the same can be done using vba by selecting the date in excel sheet.

http://www.bseindia.com/mktlive/market_summ/lonshrtpos.asp

downloads this file from the above url ( http://www.bseindia.com/bsedata/gross/2011/scbseall0706.zip)


http://www.bseindia.com/mktlive/bhavcopy.asp

downloads this file from the above url (http://www.bseindia.com/bhavcopy/eq070611_csv.zip)

kindly help me with the code for downloading the files.

Thank you in advance.
bump
 
Upvote 0
Hi all,

I got the VBA Code for the second URL

Code:
Public Sub download()

Call downloadbse1
Call downloadbsedelivery1
'Range("B3").Value = "mm/dd/yyyy"
'Range("C3").Value = "mm/dd/yyyy"
ActiveWorkbook.Save

End Sub


Public Sub downloadbse()

On Error Resume Next
MkDir ("E:\Macros\BseDailyBhav")

On Error Resume Next
Kill "E:\Macros\BseDailyBhav\*.csv*"
On Error Resume Next
Kill "E:\Macros\Input\BseDailyBhav\*.zip*"
Application.ScreenUpdating = False
Application.DisplayAlerts = False

'get start date and range===============================

Range("A1").Select

Dim daycount, daycountend, startday, startmonth, startyear As Variant
Range("B1").Value = "=C3-B3"
Range("C1").Value = Range("B3").Value
daycount = 0
daycountend = Range("B1").Value
On Error GoTo line1
While daycount <= daycountend



Range("D1").Value = Range("C1").Value + daycount
Range("E1").Value = "=TEXT(D1,""dd"")"
Range("F1").Value = "=TEXT(D1,""mm"")"
Range("G1").Value = "=TEXT(D1,""yy"")"


startday = Range("E1").Value
startmonth = Range("F1").Value
startyear = Range("G1").Value

'download file==========================================

    Dim WebUrlStr As String, LocalFile As String
    Dim oXMLHTTP As Object, bArray() As Byte, hfile As Integer
    Dim tempWb As Workbook, newWb As Workbook
    Dim MyRange As Range
        
    Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")
     
     'download the file from the web to the hardrive
    WebUrlStr = "http://www.bseindia.com/bhavcopy/eq" + CStr(startday) + "" + CStr(startmonth) + "" + CStr(startyear) + "_csv.zip"
    LocalFile = "E:\Macros\BseDailyBhav\eq" + CStr(startday) + "" + CStr(startmonth) + "" + CStr(startyear) + "_csv.zip"
    oXMLHTTP.Open "GET", WebUrlStr, False
    oXMLHTTP.send
    bArray = oXMLHTTP.ResponseBody
    hfile = 1
    Open LocalFile For Binary As #hfile
    Put #hfile, , bArray
    Close #hfile
         
    Set oXMLHTTP = Nothing
    
    
'unzip file================================================
    
    Dim FSO As Object
    Dim oApp As Object
    Dim Fname As Variant
    Dim FileNameFolder As Variant
    Dim DefPath As String

    Fname = "E:\Macros\BseDailyBhav\eq" + CStr(startday) + "" + CStr(startmonth) + "" + CStr(startyear) + "_csv.zip"
    If Fname = False Then
        'Do nothing
    Else
        'Destination folder
        DefPath = "E:\Macros\BseDailyBhav\"    '<<< Change path
        If Right(DefPath, 1) <> "\" Then
            DefPath = DefPath & "\"
        End If

        FileNameFolder = DefPath

        '        'Delete all the files in the folder DefPath first if you want
        '        On Error Resume Next
        '        Kill DefPath & "*.*"
        '        On Error GoTo 0

        'Extract the files into the Destination folder
        Set oApp = CreateObject("Shell.Application")
        oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items

       ' MsgBox "You find the files here: " & FileNameFolder

        On Error Resume Next
        Set FSO = CreateObject("scripting.filesystemobject")
        FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
    End If

   Kill "E:\Macros\BseDailyBhav\*.Zip*"
    On Error GoTo 0
    
    daycount = daycount + 1
    
    Wend

GoTo line2

line1:
MsgBox "Download Unsucessful. Kindly check Dates entered"

line2:
Range("B1:G1").ClearContents
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Range("B3").Value = "mm/dd/yyyy"
Range("C3").Value = "mm/dd/yyyy"
ActiveWorkbook.Save
End Sub


Help me with the First URL
 
Upvote 0
Hi all,

I got the VBA Code for the second URL

Code:
Public Sub download()

Call downloadbse1
Call downloadbsedelivery1
'Range("B3").Value = "mm/dd/yyyy"
'Range("C3").Value = "mm/dd/yyyy"
ActiveWorkbook.Save

End Sub





Public Sub downloadbse()

On Error Resume Next
MkDir ("E:\Macros\BseDailyBhav")

On Error Resume Next
Kill "E:\Macros\BseDailyBhav\*.csv*"
On Error Resume Next
Kill "E:\Macros\Input\BseDailyBhav\*.zip*"
Application.ScreenUpdating = False
Application.DisplayAlerts = False

'get start date and range===============================

Range("A1").Select

Dim daycount, daycountend, startday, startmonth, startyear As Variant
Range("B1").Value = "=C3-B3"
Range("C1").Value = Range("B3").Value
daycount = 0
daycountend = Range("B1").Value
On Error GoTo line1
While daycount <= daycountend



Range("D1").Value = Range("C1").Value + daycount
Range("E1").Value = "=TEXT(D1,""dd"")"
Range("F1").Value = "=TEXT(D1,""mm"")"
Range("G1").Value = "=TEXT(D1,""yy"")"


startday = Range("E1").Value
startmonth = Range("F1").Value
startyear = Range("G1").Value

'download file==========================================

    Dim WebUrlStr As String, LocalFile As String
    Dim oXMLHTTP As Object, bArray() As Byte, hfile As Integer
    Dim tempWb As Workbook, newWb As Workbook
    Dim MyRange As Range
        
    Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")
     
     'download the file from the web to the hardrive
    WebUrlStr = "http://www.bseindia.com/bhavcopy/eq" + CStr(startday) + "" + CStr(startmonth) + "" + CStr(startyear) + "_csv.zip"
    LocalFile = "E:\Macros\BseDailyBhav\eq" + CStr(startday) + "" + CStr(startmonth) + "" + CStr(startyear) + "_csv.zip"
    oXMLHTTP.Open "GET", WebUrlStr, False
    oXMLHTTP.send
    bArray = oXMLHTTP.ResponseBody
    hfile = 1
    Open LocalFile For Binary As #hfile
    Put #hfile, , bArray
    Close #hfile
         
    Set oXMLHTTP = Nothing
    
    
'unzip file================================================
    
    Dim FSO As Object
    Dim oApp As Object
    Dim Fname As Variant
    Dim FileNameFolder As Variant
    Dim DefPath As String

    Fname = "E:\Macros\BseDailyBhav\eq" + CStr(startday) + "" + CStr(startmonth) + "" + CStr(startyear) + "_csv.zip"
    If Fname = False Then
        'Do nothing
    Else
        'Destination folder
        DefPath = "E:\Macros\BseDailyBhav\"    '<<< Change path
        If Right(DefPath, 1) <> "\" Then
            DefPath = DefPath & "\"
        End If

        FileNameFolder = DefPath

        '        'Delete all the files in the folder DefPath first if you want
        '        On Error Resume Next
        '        Kill DefPath & "*.*"
        '        On Error GoTo 0

        'Extract the files into the Destination folder
        Set oApp = CreateObject("Shell.Application")
        oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items

       ' MsgBox "You find the files here: " & FileNameFolder

        On Error Resume Next
        Set FSO = CreateObject("scripting.filesystemobject")
        FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
    End If

   Kill "E:\Macros\BseDailyBhav\*.Zip*"
    On Error GoTo 0
    
    daycount = daycount + 1
    
    Wend

GoTo line2

line1:
MsgBox "Download Unsucessful. Kindly check Dates entered"

line2:
Range("B1:G1").ClearContents
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Range("B3").Value = "mm/dd/yyyy"
Range("C3").Value = "mm/dd/yyyy"
ActiveWorkbook.Save
End Sub
Help me with the First URL

bump
 
Upvote 0

Forum statistics

Threads
1,224,508
Messages
6,179,188
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top