2

私は自分の投資を追跡するために、自分自身で Excel シートを作成していました。最後の取引日の取引データの CSV ファイルを含む単一の zip ファイルがある証券取引所の Web サイトのこのリンクがあります。zip ファイルの名前は "eq_csv.zip" という形式で動的です。ここで、ddmmyy はデータが関係する取引日の日付です。市場は閉鎖されました。

最新のオンライン データを取得するために、Excel ファイルを開くたびにオンラインでチェックするモジュールを作成しました。以下のコードは、現在の日付からループして、有効な zip ファイルがダウンロードされるまで 1 日さかのぼるはずでした。たとえば、現在の日付が 4 月 28 日 (日) で、オンライン リソースのファイルが 4 月 26 日 (金) (eq260413_CSV.zip) の場合、ループは 3 回繰り返されます (2 回のファイル メッセージなしと 1 回のファイル)。ダウンロードした msg) を開き、ファイル eq260413_CSV.zip をダウンロードします。上記のオンライン リンクにファイル eq280413_CSV.zip または eq290413_CSV.zip が存在しないため、エラーが返され、ループが続行されると予想されます。コードを実行すると、関数は最初のパスでデータのないダミー ファイル eq280413_CSV.zip を作成し、iRet に値 0 を返し、ループを終了することがわかりました。

Sub DownloadFile()

Worksheets("Online Equity Data").Activate

Dim StrURL As String
Dim strPath As String
Dim dDate As Date
Dim iRet As Long

dDate = Now() + 1
iRet = 1
vFolderName = "C:\Users\Deep\Documents\Finances\Test\"

Do While iRet <> 0
    dDate = dDate - 1
    StrURL = "http://www.bseindia.com/download/BhavCopy/Equity/eq" & Format(dDate, "ddmmyy") & "_csv.zip"
    strPath = vFolderName & "eq" & Format(dDate, "ddmmyy") & "_csv.zip"
    iRet = URLDownloadToFile(0, StrURL, strPath, 0, 0)
     If iRet= 0
        MsgBox "File eq" & Format(dDate, "ddmmyy") & "_csv.zip Downloaded"
    Else
        MsgBox "No File Named eq" & Format(dDate, "ddmmyy") & "_csv.zip"  
    End If
Loop

'More code Here to unzip and import the downloaded data

End Sub()
4

1 に答える 1

2

URLDownloadToFileファイル/URL が存在しない場合、File API は使用されません。

最初に URL が有効かどうかを確認し、URLDownloadToFile該当する場合は使用する必要があります。

Leith Ross によって書かれた以下の関数を使用します (ここからピックアップ)

'Written: March 15, 2011
'Author:  Leith Ross

Public PageSource As String
Public httpRequest As Object

Function GetURLStatus(ByVal URL As String, Optional AllowRedirects As Boolean)
    Const WinHttpRequestOption_UserAgentString = 0
    Const WinHttpRequestOption_EnableRedirects = 6

    On Error Resume Next
    Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
    If httpRequest Is Nothing Then
        Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5")
    End If
    Err.Clear
    On Error GoTo 0

    httpRequest.Option(WinHttpRequestOption_UserAgentString) = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"
    httpRequest.Option(WinHttpRequestOption_EnableRedirects) = AllowRedirects

    'Clear any pervious web page source information
    PageSource = ""

    'Add protocol if missing
    If InStr(1, URL, "://") = 0 Then
        URL = "http://" & URL
    End If

    'Launch the HTTP httpRequest synchronously
    On Error Resume Next
    httpRequest.Open "GET", URL, False
    If Err.Number <> 0 Then
      'Handle connection errors
        GetURLStatus = Err.Description
        Err.Clear
        Exit Function
    End If
    On Error GoTo 0

    'Send the http httpRequest for server status
    On Error Resume Next
    httpRequest.Send
    httpRequest.WaitForResponse
    If Err.Number <> 0 Then
      ' Handle server errors
        PageSource = "Error"
        GetURLStatus = Err.Description
        Err.Clear
    Else
      'Show HTTP response info
        GetURLStatus = httpRequest.Status & " - " & httpRequest.StatusText
      'Save the web page text
        PageSource = httpRequest.responsetext
    End If
    On Error GoTo 0
End Function

URLがOKの場合、このようなものが得られます

ここに画像の説明を入力

そうでない場合は、このようなものが得られます

ここに画像の説明を入力

したがって、必要なのは検索する200 - OKだけで、それが見つかった場合は、を使用しURLDownloadToFileてファイルをダウンロードします。

于 2013-04-30T00:48:34.993 に答える