Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15621

VBA store data from file in web to matrix

$
0
0
Hello,

I was trying to get data from the web https://www.omie.es/es/file-access-l...r=marginalpdbc. The idea is to get data from all the files between to dates, the files are presented with the first line as a header and a character "*" as end of the current file. Data is separated by ";" and it normally has 26 lines (header, 24 lines of relevant data and the end character), but there are files with 25 or 27 (days where hour is change in this country). Also there are files in .zip, that comprise all the files of the days of a past year (2020,2019,2018...), and I do not know if it can be read as it is.

I do not have a lot of experience in this field of VB, and I try different things, but I have the problem that my code is very slow. When I want to get data for 1 month (30/31 files) it last almost 3 minutes! I will like to get it below 30 seconds if possible. These are the versions, that i manage to make work.

Version 1 (Download .zip and read files online):
Code:

VBA Code:
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
    Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
    ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Sub HTML_VBA_Extract_Data_From_Website_To_Excel()
    Dim oXMLHTTP As Object
    Dim sPageHTML  As String
    Dim sURL As String
    Dim fechaInicio, fechaFin As Date
    srcpath = "https://www.omie.es/en/file-download?parents%5B0%5D=marginalpdbc&filename="
    dlpath = ThisWorkbook.Path & "\"
    fechaInicio = Range("B1")
    fechaFin = Range("B2")
    Diff = DateDiff("d", fechaInicio, fechaFin)
    diain = Format(fechaInicio, "dd")
    mesIn = Format(fechaInicio, "mm")
    añoIn = Format(fechaInicio, "yyyy")
    Dim matriz
    ReDim matriz(24 * (Diff + 3), 2)
    Do While añoIn < Format(Now(), "yyyy") And añoIn <= Format(fechaFin, "yyyy")
        Name = "marginalpdbc_" + añoIn + ".zip"
        txtsrc = srcpath + Name
        URLDownloadToFile 0, txtsrc, dlpath & Name, 0, 0
        fechaInicio = DateAdd("yyyy", 1, fechaInicio)
        diain = "01"
        mesIn = "01"
        añoIn = Format(fechaInicio, "yyyy")
    Loop
    f = 1
    Do While (fechaFin >= fechaInicio)
        Name = "marginalpdbc_" + añoIn + mesIn + diain + ".1"
        txtsrc = srcpath + Name
        Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
        oXMLHTTP.Open "GET", txtsrc, False
        oXMLHTTP.send
        sPageHTML = oXMLHTTP.responseText
        aux = Split(sPageHTML, ";")
        i = f
        For f = f To f + 23
            matriz(f - 1, 1) = aux(6 * (f - i) + 5)
            matriz(f - 1, 2) = aux(6 * (f - i) + 6)
            End If
        Next
        f = f + 1
        fechaInicio = DateAdd("d", 1, fechaInicio)
        diain = Format(fechaInicio, "dd")
        mesIn = Format(fechaInicio, "mm")
        añoIn = Format(fechaInicio, "yyyy")
    Loop
End Sub

Version 2 (Download .zip and files, then read files):
Code:

Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
    Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
    ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Sub HTML_VBA_Extract_Data_From_Website_To_Excel()
    Dim oXMLHTTP As Object
    Dim sPageHTML  As String
    Dim sURL As String
    Dim fechaInicio, fechaFin As Date
    srcpath = "https://www.omie.es/en/file-download?parents%5B0%5D=marginalpdbc&filename="
    dlpath = ThisWorkbook.Path & "\"
    fechaInicio = Range("B1")
    fechaFin = Range("B2")
    Diff = DateDiff("d", fechaInicio, fechaFin)
    diain = Format(fechaInicio, "dd")
    mesIn = Format(fechaInicio, "mm")
    añoIn = Format(fechaInicio, "yyyy")
    Dim matriz
    ReDim matriz(24 * (Diff + 3), 2)
    Do While añoIn < Format(Now(), "yyyy") And añoIn <= Format(fechaFin, "yyyy")
        Name = "marginalpdbc_" + añoIn + ".zip"
        txtsrc = srcpath + Name
        URLDownloadToFile 0, txtsrc, dlpath & Name, 0, 0
        fechaInicio = DateAdd("yyyy", 1, fechaInicio)
        diain = "01"
        mesIn = "01"
        añoIn = Format(fechaInicio, "yyyy")
    Loop
    f = 1
    Do While (fechaFin >= fechaInicio)
        Name = "marginalpdbc_" + añoIn + mesIn + diain + ".1"
        txtsrc = srcpath + Name
        Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
        oXMLHTTP.Open "GET", txtsrc, False
        oXMLHTTP.send
        sPageHTML = oXMLHTTP.responseText
        aux = Split(sPageHTML, ";")
        i = f
        For f = f To f + 23
            matriz(f - 1, 1) = aux(6 * (f - i) + 5)
            matriz(f - 1, 2) = aux(6 * (f - i) + 6)
            End If
        Next
        f = f + 1
        fechaInicio = DateAdd("d", 1, fechaInicio)
        diain = Format(fechaInicio, "dd")
        mesIn = Format(fechaInicio, "mm")
        añoIn = Format(fechaInicio, "yyyy")
    Loop
End Sub

So, if somebody could help finding a better method or fixing my code I would be very grateful. Thanks for your time

Viewing all articles
Browse latest Browse all 15621

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>