Excel VBA错误从CSV导入数据

时间:2017-06-01 21:03:51

标签: excel vba

我正在尝试运行以下VBA代码。它适用于前两个文件,但不适用于第三个文件。我不知道为什么文件存在。我检查了路径。

以下是debug.print的代码和输出:

Sub readData(folderName As String, fileName As String, worksheetName As String)
    Dim connectionLocation As String

    connectionLocation = "TEXT;" & folderName & fileName

    Debug.Print (connectionLocation)
    With Sheets(worksheetName).QueryTables.Add(Connection:=connectionLocation, _
        Destination:=Sheets(worksheetName).Range("A1"))
        .Name = worksheetName
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = xlMacintosh
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .Refresh BackgroundQuery:=False
        .UseListObject = False

    End With
End Sub


TEXT;/Users/gryslik/gamaCode/engineAnalysis/model/temp/enginePortfolio.csv
TEXT;/Users/gryslik/gamaCode/engineAnalysis/model/temp/inflatedData.csv
TEXT;/Users/gryslik/gamaCode/engineAnalysis/model/temp/uninflatedData.csv

按要求,这是uninflatedData.csv的最后几行:

2025-10-01,6.99845608812112,6.99845608812112,2.37765391176136,0.550630416325757,0.550630416325757,0.91703100026816,0.519412428409091,0.519412428409091,1.00986157034952,1.00986157034952,0.91703100026816,0.91703100026816,0,1.74174518596591,1.74174518596591,2.012878005,1.15193822842105,2.15190176814235,2.15190176814235,2.58594116368453,2.15190176814235,7.40086014228261,7.40086014228261,0.2677056328125,2.6711781271875,2.24673667094063,15.8590859130978,15.8590859130978,15.8590859130978,15.8590859130978,15.8590859130978,0.913960283203125,5.18232955708875,4.35965109450623,4.35965109450623
2025-11-01,6.9685481561206,6.9685481561206,2.36694375900568,0.543571052013889,0.543571052013889,0.908986868686862,0.502098680795455,0.502098680795455,1.00100313552189,1.00100313552189,0.908986868686862,0.908986868686862,0,1.72646671942235,1.72646671942235,1.9982919325,1.13913891477193,2.12799174849632,2.12799174849632,2.55720848408804,2.12799174849632,7.36923253483696,7.36923253483696,0.26274812109375,2.64149837021875,2.2217729301524,15.7976165878532,15.7976165878532,15.7976165878532,15.7976165878532,15.7976165878532,0.903805168945312,5.16325063816739,4.34360089375361,4.34360089375361
2025-12-01,6.93864022412009,6.93864022412009,2.35623360625,0.53651168770202,0.53651168770202,0.900942737105564,0.484784933181818,0.484784933181818,0.992144700694267,0.992144700694267,0.900942737105564,0.900942737105564,0,1.71118825287879,1.71118825287879,1.98370586,1.1263396011228,2.1040817288503,2.1040817288503,2.52847580449154,2.1040817288503,7.3376049273913,7.3376049273913,0.257790609375,2.61181861325,2.19680918936417,15.7361472626087,15.7361472626087,15.7361472626087,15.7361472626087,15.7361472626087,0.8936500546875,5.14417171924604,4.327550693001,4.327550693001
2026-01-01,6.90873229211957,6.90873229211957,2.34552345349432,0.529452323390151,0.529452323390151,0.892898605524266,0.467471185568182,0.467471185568182,0.983286265866639,0.983286265866639,0.892898605524266,0.892898605524266,0,1.69590978633523,1.69590978633523,1.9691197875,1.11354028747368,2.08017170920427,2.08017170920427,2.49974312489505,2.08017170920427,7.30597731994565,7.30597731994565,0.25283309765625,2.58213885628125,2.17184544857594,15.6746779373641,15.6746779373641,15.6746779373641,15.6746779373641,15.6746779373641,0.883494940429687,5.12509280032468,4.31150049224838,4.31150049224838

0 个答案:

没有答案