I am getting a very strange error in VBA (1004) when trying to import a csv file.
Here is my code:
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & ThisWorkbook.Path & "/" & "IJR" & ".csv", Destination _
:=Range("$A$1"))
.Name = "IJR"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlMacintosh
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(3, 9, 9, 9, 9, 9, 1)
.Refresh BackgroundQuery:=False
.UseListObject = False
End With
It gives me the error on .Refresh BackgroundQuery:=False.
IJR.csv IS a file in the same directory as my workbook. I have a bunch of csv files in the directory. The strange thing is, when I change "IJR" to "AA" or "HES" (which are also csv files in the directory), the code works. Most of the csv filenames do not work ("USO.csv", "AAL.csv", "AAPL.csv", "HD.csv", etc.), however a few of them do (so far just "AA.csv" and "HES.csv"). Theyre all identically formatted csv files with the same number of columns. I am completely at a loss. Maybe I'm making some bonehead error, but I can't spot it.
Another bit of useful info: I am running Excel 2016 for Mac. I have a very similar piece of code that runs if the OS is Windows (due to small differences in the VBA code for Mac and Windows). It works for every csv file, no issue. That code, for reference, is:
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & ThisWorkbook.Path & "\" & symb & ".csv", Destination _
:=Range("$A$1"))
.Name = symb
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(3, 9, 9, 9, 9, 9, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Update 3/10/16: I got it to work in Excel 2011 for Mac (by changing the "/" in the path to Application.PathSeparator), but it still does the same thing in Excel 2016 for Mac. May just be a bug in the new Excel.
QueryTable)? Do they open? Look correct?