【问题标题】:Excel Macro To import CSV data fails with TextFileParseTypeExcel 宏导入 CSV 数据失败,TextFileParseType
【发布时间】:2019-12-02 18:19:57
【问题描述】:

我正在尝试使用以下代码将 CSV 数据从 URL 加载到我的 MS Excel 工作表中:

Sub LoadCSVData()
    Dim sSheetName As String
    sSheetName = "Sheet1"

    ActiveWorkbook.Sheets(sSheetName).UsedRange.ClearContents

    With ActiveWorkbook.Sheets(sSheetName).QueryTables.Add(Connection:= _
        "URL;https://sampledomain.com/mydata.csv" _
        , Destination:=Sheets(sSheetName).Range("$A$1"))
        .TextFileParseType = xlDelimited
        .TextFileCommaDelimiter = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .Refresh BackgroundQuery:=False
    End With

End Sub

它抛出了

错误:1004 应用程序定义或对象定义错误

TextFileParseTypeTextFileCommaDelimiter 一致。 当我将它们注释掉时,宏可以工作,但所有内容都加载到A1 单元格中并且没有被解析。

知道怎么解决吗?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    这几乎是 Excel QueryTables.Add from URL Comma Delimited 的副本

    将“URL”更改为“TEXT” 这是我对您的代码的快速修改:

    Sub LoadCSVData()
        Dim sSheetName As String
        sSheetName = "Sheet1"
        Dim myTable As QueryTable
        ActiveWorkbook.Sheets(sSheetName).UsedRange.ClearContents
    
    Set myTable = ActiveWorkbook.Sheets(sSheetName).QueryTables.Add(Connection:= _
            "TEXT;http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv" _
            , Destination:=Sheets(sSheetName).Range("$A$1"))
    
        With myTable
            .TextFileCommaDelimiter = True
            .TextFileParseType = xlDelimited
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .Refresh BackgroundQuery:=False
        End With
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2012-02-09
      • 2016-04-06
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      • 2020-09-28
      • 2021-10-09
      • 1970-01-01
      • 2017-07-27
      相关资源
      最近更新 更多