【问题标题】:Run-time error '5' upon CSV import (Excel 2011, Mac)CSV 导入时出现运行时错误“5”(Excel 2011,Mac)
【发布时间】:2015-12-25 19:36:03
【问题描述】:

我一直在抓取该网站以解决我的问题。我在 Mac OS X 10.10.5 上使用 Excel 2011。

我的情况是这样的: 我有一堆 CSV 文件需要导入 Excel 以进行进一步的统计分析。这是我的一个 CSV 文件(与谷歌驱动器共享)的example。 数据用逗号分隔,并且应该导入到所有工作表中的单元格 A1(为了澄清,我不希望所有数据都在 A1 中。现在这很愚蠢,不是吗。CSV 数据应该从这里开始,并且跨越 A 列和 B 列,直到行号 ~1200 或任何长度)。导入给定 CSV 文件的工作表应以 CSV 文件命名(不带“.csv”),因为稍后我将使用工作表名称调用数据。 使用导入向导非常乏味,而且要导入 180 个,VBA 代码/宏会很有帮助,因为我需要 6 个非常专注的小时(而且我喜欢在 excel 中做一些聪明的事情)

目前我有一个添加新工作表的代码,但它不起作用

(1) 未导入数据 - 我收到运行时错误“5” - 过程调用或参数无效。

(2) 工作表以文件类型扩展名 .csv 命名。

关于为什么我在此之后收到错误的任何想法?:

With ActiveSheet.QueryTables.Add( _
        Connection:="TEXT;" & Fname, _
        Destination:=Range("A1"))l

当前代码:

Sub CSVIMPORTTEST2()
Dim MyPath As String
Dim MyScript As String
Dim MyFiles As String
Dim MySplit As Variant
Dim N As Long
Dim Fname As String
Dim mybook As Worksheet
On Error Resume Next
MyPath = MacScript("return (path to documents folder) as String")
'Or use MyPath = "Macintosh HD:Users:YourUserName:Desktop:TestFolder:"

MyScript = "set applescript's text item delimiters to (ASCII character 10) " & vbNewLine & _
    "set theFiles to (choose file of type " & _
  " (""public.comma-separated-values-text"") " & _
    "with prompt ""Please select a file or files"" default location alias """ & _
    MyPath & """ multiple selections allowed true) as string" & vbNewLine & _
    "set applescript's text item delimiters to """" " & vbNewLine & _
    "return theFiles"

MyFiles = MacScript(MyScript)
On Error GoTo 0

If MyFiles <> "" Then
With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With
MySplit = Split(MyFiles, Chr(10))
For N = LBound(MySplit) To UBound(MySplit)

    'Get file name only and test if it is open
    Fname = Right(MySplit(N), Len(MySplit(N)) - InStrRev(MySplit(N), _
    Application.PathSeparator, , 1))

        Set mybook = Nothing
            On Error Resume Next
            Set mybook = Sheets.Add(After:=Sheets(Worksheets.Count))
            mybook.Name = Fname
            On Error GoTo 0
     Next

Worksheets(Fname).Activate

With ActiveSheet.QueryTables.Add( _
    Connection:="TEXT;" & Fname, _
    Destination:=Range("A1"))
.Name = "CSV" & Worksheets.Count + 1
.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
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
End With

      End If

End Sub

希望有人能帮忙

最好的问候埃米尔霍克

【问题讨论】:

  • 失败时Fname的值是多少?它是完整路径吗?
  • 看起来Fname 只包含文件名而不包含文件的完整路径,在这种情况下 Excel 将不知道文件在哪里!

标签: excel vba macos csv excel-2011


【解决方案1】:

我不熟悉 VBA 代码或您正在使用的 Excel 参数,但您可能需要检查一些事项:

首先,您可以对文件名和工作表名称进行调试打印 - 只是为了确保(尤其是 Fname)。

现在是 csv 文件 - 第一行看起来像这样(不要介意这里的特殊字符.. - 这些在文件中是可以的,UTF-8):

DATE,2015-11-30 08:30:36
SAMPLE RATE,1

BAR 1: Kløe
Range: 0 - 100
Labels: Ingen kløe - null - Værst tænkelige kløe

TIME,VALUE
0,0.0
1,0.0

及参数:

.FieldNames = True
.TextFileStartRow = 1
.TextFileCommaDelimiter = True

但您的字段名从第 8 行开始,数据从第 9 行开始,而您希望该数据从单元格 A1 开始。也许你应该在这里调整参数。

另外,在第 610 行附近还有另一个标题:

600,63.0
601,63.0
BAR 2: Smerte
Range: 0 - 100
Labels: Ingen smerte - null - Værst tænkelige smerte

TIME,VALUE
0,0.0
1,0.0

您可能也不希望在您的数据中使用它。

不知道这是什么意思,但如果你只有 2 列看起来很奇怪:

.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 2012-10-14
    • 1970-01-01
    • 2021-02-25
    相关资源
    最近更新 更多