CSV导入时运行时错误“5”(Excel 2011,Mac)

我一直在爬网站寻求解决我的问题。 我使用Excel 2011 for Mac OS X 10.10.5。

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

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

(1)数据没有被导入 – 我得到一个运行时错误'5' – 无效的过程调用或参数。

(2)表格以文件types扩展名.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 

希望有人能够帮助

最好的问候Emil Hoeck

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

首先,你可以做一个debugging打印文件名和工作表的名字 – 只是为了确保(尤其是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)