【问题标题】:Open .csv file from internet using VBA使用 VBA 从 Internet 打开 .csv 文件
【发布时间】:2020-12-27 23:45:12
【问题描述】:

我正在尝试从 Internet 打开一个 VBA 文件。我首先尝试使用this method成功下载并打开它。但是,我不希望文件存储在我的硬盘上。因此,我尝试了一种更简单的方法,即使用 workbooks.open 方法:

Sub OpenInternetFile()

Workbooks.Open _
    Filename:="http://webstat.banque-france.fr/fr/downloadFile.do?id=5385698&exportType=csv", _
    Format:=4

End Sub

问题,下载的文件(见图1)看起来不是.csv格式;其数据无法正确转换。图片 2 是预期的渲染。请问您知道如何解决这个问题吗?

图片1:

图片2:

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    Excel 似乎错误地使用逗号而不是分号作为分隔符。我已尝试按照 BigBen 的建议修改格式选项,但未成功。

    您可以尝试以下代码。绝对不是最优雅的方式,但它似乎有效。

    Sub ImportData()
        Dim Wkb As Workbook
        Dim Wks As Worksheet
        Dim MaxColumn As Long
        Dim MaxRow As Long
        Dim i As Long
        Dim j As Long
        Dim k As Long
        Dim TmpStr As String
        Dim TmpSplit() As String
        Dim TmpArr() As String
        
        
        Set Wkb = Workbooks.Open(Filename:="http://webstat.banque-france.fr/fr/downloadFile.do?id=5385698&exportType=csv") 
        Set Wks = Wkb.Sheets(1)
        With Wks
            MaxColumn = .UsedRange.Columns.Count 
            MaxRow = .UsedRange.Rows.Count 
            Application.Calculation = xlCalculationManual
        'The TmpArr array is used to temporarily store records, and dimensionned according to the data size
            ReDim TmpArr(1 To MaxRow, 1 To MaxColumn)
            'For each row in imported data
        For i = 1 To MaxRow
            'Concatenate each column, with a comma inbetween
                TmpStr = vbNullString
                TmpStr = .Cells(i, 1)
                For j = 2 To MaxColumn
                    TmpStr = TmpStr & "," & .Cells(i, j)
                Next j
            'Next, split the concatenated string and store in the TmpSplit array, which holds each value for the current record
                TmpSplit = Split(TmpStr, ";")
                'The TmpSplit array is then used to fill the TmpArr, which contains each record
                For k = 0 To UBound(TmpSplit) - 1
                    TmpArr(i, k + 1) = TmpSplit(k)
                Next k
            Next i
    
        'Finally, print the TmpArr in the current sheet. The range on which i print the record is dynamically dimensionned using the size of the TmpArr
            .UsedRange.Clear
            .Range("A1:" & .Cells(UBound(TmpArr, 1), UBound(TmpArr, 2)).Address) = TmpArr
            Application.Calculation = xlCalculationAutomatic
        End With
    End Sub
    

    【讨论】:

    • 你的代码就像一个魅力。但是,我不明白它是如何工作的(我想编码对你来说不仅仅是一种爱好),如果我对它不满意,我会使用这样的脚本来困扰我......我试图在打开文件之前更改系统的默认分隔符但它不起作用:(
    • 很高兴它成功了!为了更清晰,我添加了一些 cmets。
    • 非常感谢您的工作。我在逐步运行代码时使用了“局部变量”和“立即”窗口,很高兴看到您的 cmets 与我的理解相匹配。我需要在数组上做更多的工作;我从未使用过它们,并且很惊讶它们使用“字符串”变量工作。
    【解决方案2】:

    请尝试简单地使用OpenText 而不是Open,用于“;”作为分隔符:

    Dim  pathToFile As String
      pathToFile = "http://webstat.banque-france.fr/fr/downloadFile.do?id=5385698&exportType=csv"
      Workbooks.OpenText fileName:=pathToFile, origin:=xlWindows, StartRow:=1, _
                               DataType:=xlDelimited, Other:=True, OtherChar:=";"
    

    如果某些列格式不适合您的本地化,可以使用FieldInfo 轻松设置每一列的格式。

    【讨论】:

    • @Jean-FIC:很高兴我能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多