运行时错误';5';导入CSV后(Excel 2011,Mac)

运行时错误';5';导入CSV后(Excel 2011,Mac),excel,vba,macos,csv,excel-2011,Excel,Vba,Macos,Csv,Excel 2011,我一直在网站上搜寻我的问题的解决方案。我使用Excel 2011 for Mac OS X 10.10.5 我的情况是: 我有一堆CSV文件,需要导入Excel进行进一步的统计分析。这是我的一个CSV文件(与google drive共享)的示例。 数据以逗号分隔,应导入所有工作表中的单元格A1(为了澄清,我不希望所有数据都在A1中。现在这太傻了,不是吗。CSV数据应从这里开始,跨越A列和B列,一直到行号~1200或任意长度)。导入给定CSV文件的工作表应以CSV文件命名(不带“.CSV”),因为

我一直在网站上搜寻我的问题的解决方案。我使用Excel 2011 for Mac OS X 10.10.5

我的情况是: 我有一堆CSV文件,需要导入Excel进行进一步的统计分析。这是我的一个CSV文件(与google drive共享)的示例。 数据以逗号分隔,应导入所有工作表中的单元格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
Sub-CSVIMPORTTEST2()
将MyPath设置为字符串
将MyScript设置为字符串
将MyFile设置为字符串
Dim MySplit作为变体
长
作为字符串的Dim Fname
将mybook设置为工作表
出错时继续下一步
MyPath=MacScript(“返回(文档文件夹的路径)为字符串”)
'或使用MyPath=“Macintosh HD:Users:YourUserName:Desktop:TestFolder:”
MyScript=“将applescript的文本项分隔符设置为(ASCII字符10)”&vbNewLine&_
“将文件设置为(选择文件类型”&”_
(“public.逗号分隔值文本”)&_
提示“请选择一个或多个文件”“默认位置别名”“&”_
MyPath&“允许多选为true)作为字符串(&vbNewLine&_
将applescript的文本项分隔符设置为“”&vbNewLine&_
“返回文件”
MyFiles=MacScript(MyScript)
错误转到0
如果我的文件是“”,那么
应用
.ScreenUpdate=False
.EnableEvents=False
以
MySplit=Split(MyFiles,Chr(10))
对于N=LBound(MySplit)到UBound(MySplit)
'仅获取文件名并测试它是否打开
Fname=Right(MySplit(N),Len(MySplit(N))-InStrRev(MySplit(N)_
Application.PathSeparator,1))
设置mybook=Nothing
出错时继续下一步
设置mybook=Sheets.Add(之后:=Sheets(Worksheets.Count))
mybook.Name=Fname
错误转到0
下一个
工作表(Fname)。激活
使用ActiveSheet.QueryTables.Add(_
连接:=“TEXT;”&Fname_
目的地:=范围(“A1”))
.Name=“CSV”&工作表。计数+1
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.refreshinfoleopen=False
.BackgroundQuery=True
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.TextFilePromptOnRefresh=False
.TextFilePlatform=xlMacintosh
.TextFileStartRow=1
.TextFileParseType=xlDelimited
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=False
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=True
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(1,1,1,1,1,1,1,1)
以
如果结束
端接头
希望有人能帮忙


向Emil Hoeck致意我不熟悉VBA代码或您正在使用的Excel参数,但您可能需要检查以下几点:

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

现在,对于csv文件-第一行如下所示(不要介意此处的特殊字符…-这些在文件UTF-8中是可以的):

以及参数:

.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
您可能也不希望在数据中出现这种情况

不知道这意味着什么,但如果只有两列,则看起来很奇怪:

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

我不熟悉VBA代码或您正在使用的Excel参数,但您可能需要检查以下几点:

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

现在,对于csv文件-第一行如下所示(不要介意此处的特殊字符…-这些在文件UTF-8中是可以的):

以及参数:

.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
您可能也不希望在数据中出现这种情况

不知道这意味着什么,但如果只有两列,则看起来很奇怪:

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

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