Run-time error '5' upon CSV import (Excel 2011, Mac) - excel

I have been crawling the site for solutions to my problem. I use Excel 2011 for Mac OS X 10.10.5.
My situation is this:
I have a bunch of CSV files that i need to import into Excel for further statistical analysis. Here is an example of one of my CSV files (shared with google drive).
The data is delimited by comma, and should be imported to cell A1 in all sheets (for clarification, I do not wish to have all data in A1. That would be silly now, wouldn't it. CSV data should start here, and span across column A and B, down to row number ~1200 or whatever length it will be). The sheet a given CSV file is imported to should be named after the CSV file (without ".csv") as I will be calling data later by using the sheet names.
Using the import wizard is extremely tedious, and with 180 imports coming up, a VBA code / macro would help a lot as it would take me 6 very focused hours (and I like to do smart stuff in excel)
Currently I have a code which adds new sheets, but it does not work as
(1) Data is not imported - I get a runtime error '5' - Invalid procedure call or argument.
(2) Sheets are named with the file type extension .csv.
Any ideas as to why I get an error after this?:
With ActiveSheet.QueryTables.Add( _
Connection:="TEXT;" & Fname, _
Destination:=Range("A1"))l
Current code:
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
Hope someone out there is able to help
Best regards Emil Hoeck

I'm not familiar with VBA-code or the parameters for Excel you're using, but you might want to check a few things:
First, you could do a debugprint of the filename(s) and the names of the sheets - just to make sure (especially Fname).
Now for the csv-file - the first lines look like this (don't mind the special characters here.. - these are ok in the file, 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
and the parameters:
.FieldNames = True
.TextFileStartRow = 1
.TextFileCommaDelimiter = True
but your fieldnames start on row 8, and the data at row 9, while you want that data to start in cell A1. Maybe you should tune the parameters here.
Also, around line 610 there's another header:
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
You probably don't want that in your data either.
Don't know what this means, but looks strange if you only have 2 columns:
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

Related

Unnecessary value concatenation and Type Errors in Excel VBA

My VBA code takes a .txt file from a specific software output (Carlson Survey software) and does some calculations, then converts it into a .CSV file. I am specifically having issues with the calculation component, where one of my columns of the text file (brought into excel using comma separators) isn't doing the calculation I tell it, and is seemingly concatenating itself (removes everything after the decimal point). My assumption is, that because I am taking these values into an Array (which had to be set as string, or else I was getting type errors) which is set as a string, this is causing the concatenation after the decimal point. I am at a loss as to why the calculation doesn't appear to be running though, as the program seemingly executes fine.
And the VBA script for quick reference (specific section with problem is the 'Do data conversion' section:
Private Sub Workbook_Open()
Sheets("Sheet1").Cells.ClearContents
'---------------------------------------------------------------------------------------
'Choose and open the .TXT file for conversion
Dim answer As Integer
answer = MsgBox("Do you want to process a .TXT file for use in InfoSWMM?", vbYesNo + vbQuestion, "Select .TXT File")
If answer = vbNo Then
Exit Sub
End If
Dim Ret
Ret = Application.GetOpenFilename("Text Files (*.txt),*.txt")
If Ret <> False Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Ret, Destination:=Range("$A$1") _
)
.Name = "Sample"
.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 = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If
'---------------------------------------------------------------------------------------
'Do data conversion, SECTION NEEDS UPDATING LACKING FEATURES, BUGS
Dim row As Integer
Dim col As Integer
Dim i As Integer
Dim tester(3) As String 'Bug[1] related, type error (see below). String type fixes type error, but causes undesired concatenation
Dim col_test As Integer
Dim rim As Integer
For row = 1 To ActiveSheet.UsedRange.Rows.Count
If IsEmpty(ActiveSheet.Cells(row, 1).Value) = True Then
Exit For
End If
'Change these values in case feature code library is changed in Carlson, also need to add extra fields
If ActiveSheet.Cells(row, 5).Value = "SD" Or ActiveSheet.Cells(row, 5).Value = "WQ" Then
col_test = 20
rim = ActiveSheet.Cells(row, 4).Value
For i = 0 To 3
tester(i) = ActiveSheet.Cells(row, col_test).Value 'Bug[1] here, type error if not a String.
col_test = col_test + 4
Next i
ActiveSheet.Cells(row, 37).Value = rim - Application.Max(tester) 'Bug[2] here, not performing calculation.
End If
Next row
'---------------------------------------------------------------------------------------
'Save converted file as .CSV
MsgBox "Choose the desired save location for the .CSV file."
Dim InitialName As String
Dim PathName As Variant
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
InitialName = "sfm_output"
PathName = Application.GetSaveAsFilename(InitialFileName:=InitialName, fileFilter:="CSV (Comma delimited) (*.csv), *.csv")
ws.Copy
ActiveWorkbook.SaveAs Filename:=PathName, _
FileFormat:=xlCSV, CreateBackup:=False
MsgBox "Process completed successfully." & vbNewLine & "File saved to:" & vbNewLine & PathName
'---------------------------------------------------------------------------------------
'Close all Workbooks
Application.DisplayAlerts = False
Application.Quit
End Sub
Any help is greatly appreciated. Thanks.
Have you tried CSTRING or CINT functions?
For example:
tester(i) = CString(ActiveSheet.Cells(row, col_test).Value)

How to look up files (.txt) in a folder, import delimited data from file into excel, then move txt file to archive?

I've got a folder that will be populated by files with common characters but with a single digit variance, i.e. Test 34, or Test 40. The folder can be empty or contain 50 files.
I'm trying to write code that can:
go to folder check if folfer is empty, if not
take each text file and import space delimited data into excel (ideally based on date of file modification in folder - haven't gotten that far yet.)
Move files to archive folder (may cause me issues if file name already exists in archive)
if folder empty, exit/end sub
I've tried various methods, however my VBA knowledge isn't great. Below is a version of my code that has been spliced with various code from stackoverflow
Sub ImportFiles()
Dim eRow As String
Dim fileName As String
Dim rowNumber As String
Dim outputSheet As String
Dim sheetName As String
Dim folder As String
Dim strPath As Variant
Dim i As Integer
Dim fCount As Long
folder = "U:\Projects\Raw data\"
outputSheet = "Dataset"
fCount = UBound(Filter(Split(CreateObject("WScript.Shell").Exec("CMD /C DIR """ & folder & "*.*"" /S /B /A:-D").StdOut.ReadAll, vbCrLf), ".")) + 1
'MsgBox Format(fCount, "#,00") & " files were found."
If Dir(folder & "*.*") = "" Then
MsgBox "The folder doesn't contain (visible) files"
MsgBox "Bye Bye!!"
Exit Sub
Else
'MsgBox "The folder does contain (visible) files"
For i = 0 To 11
fileName = folder & "Test" & i & ".txt"
'If Dir(fileName) = "" Then
eRow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With Sheets(outputSheet).QueryTables.Add(Connection:="TEXT;" + fileName, Destination:=Sheets(outputSheet).Range("$A$" + eRow))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Dim wb_conection As WorkbookConnection
For Each wb_Connection In ActiveWorkbook.Connections
If InStr(fileName, wb_Connection.Name) > 0 Then
wb_Connection.Delete
End If
Next wb_Connection
Next i
End If
MsgBox ("Done")
End Sub

VBA code error: "out of memory"

i'm currently working on a project where Excel automatically fetches financial data of publicly traded companies.
Sometimes I get the error:"out of memory". Is there a way to fix this?
I'm using 64-bit Excel.
Code:
Sub Get_IS1()
Dim x As Integer
x = 0
execute:
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Dim ws As Worksheet
Set ws = Sheets("Summary")
ws.Activate
Dim qurl, symbol As String
ticker = ws.Range("C9").Value
Exchange = ws.Range("C8").Value
'Delete Prior Connections
For Each cn In ThisWorkbook.Connections
cn.Delete
Next cn
'Clear Prior Data
Sheets("COMP1").Activate
Sheets("COMP1").Cells.Clear
'URL
qurl = "http://financials.morningstar.com/ajax/ReportProcess4CSV.html?&t=" & Exchange & ":" & ticker & "&region=usa&culture=en-US&cur=&reportType=is&period=12&dataType=A&order=asc&columnYear=5&curYearPart=1st5year&rounding=3&view=raw&r=618279&denominatorView=raw&number=3"
'Get Data Via Text File
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & qurl & "" _
, Destination:=Sheets("COMP1").Range("B1"))
.Name = _
"Table 1"
.FieldNames = True
.PreserveFormatting = False
.RefreshStyle = xlInsertDeleteCells
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
On Error GoTo ends
.Refresh BackgroundQuery:=False
End With
Set ticker = Nothing
Set Exchange = Nothing
Set qurl = Nothing
Set ws = Nothing
Get_BS1
Exit Sub
'Error Handle for Invalid Entry
ends:
x = x + 1
If x = 5 Then
MsgBox ("No response was recived from Morningstar. Either an invalid ticker was entered or no prior records exist for the chosen symbol.")
ws.Activate
ElseIf x < 5 Then
GoTo execute
End If
End Sub
This piece of code fetches the Income Statement, Get_BS1 is called to get the Balance Sheet and after that the Cash Flow Statement
This happens to me when I am attempting to refresh against an empty file. How peculiar that the thing that should use the least memory appears to use the most...
In your case the text is coming from a webpage. You would have to check the result up front before you run your query.
Let's check one thing real quick. Change your connection string to a URL instead of TEXT:
'Get Data Via Text File
With ActiveSheet.QueryTables.Add(Connection:="URL;" & qurl & "" _

Excel Date formatting with hash marks for some reason

The Problem
I've got a VBA program that exports data from one Excel file into a CSV. When it comes across a date, it formats it like #2016-06-14#. I'm assuming the hash marks (or octothorpe or pound sign or hashtag) are meant to indicate that the field is a date field. But, when I'm importing the CSV back into a different Workbook, the date will not come in no matter how I format the field. It still contains the # characters.
The Question
How can I get the date column to import as a YMD format date?
Appendix
Here's some code I'm using to export and import, for reference.
Export
Sub WriteCSV(writeRange As Range, fileName As String)
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
myFile = ActiveWorkbook.Path & "\Results\" & fileName & ".csv"
Debug.Print myFile
Open myFile For Output As #1
For i = 1 To writeRange.Rows.Count
For j = 1 To writeRange.Columns.Count
cellValue = writeRange.Cells(i, j).value
If j = writeRange.Columns.Count Then
Write #1, cellValue
Else
Write #1, cellValue,
End If
Next
Next
Close #1
End Sub
Import
Sub ReadCSV(targetCell As Range, filePath As String)
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & filePath, Destination:=targetCell)
.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(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
.Delete
End With
End Sub
Try outputing .text instead of .value.
Change this:
cellValue = writeRange.Cells(i, j).value
to this:
cellValue = writeRange.Cells(i, j).Text
Discard those #.
When I manually Save as a file as CSV, Excel does NOT put those # around the dates. Instead it writes the dates as they are defined in my regional settings (that is - for me - dd/mm/yyyy).
And when importing back, the recorder used array(...4...) for the date column, and it was imported correctly.
Anyway CSV really sucks if your are working in an international environment since it behaves differently according the the local machine settings. Being poorly defined, it's the last format I would use.

Using a variable or string as a file name for importing data using query tables

I'm trying to import a lot of text files with numerical names into a separate worksheets.
The loop to create the worksheets works fine
Dim i as integer 'initial file name
Dim k as integer 'final file name
i = Cells(3, 3).Value
k = Cells(5, 3).Value
Do while i <= k
Worksheets.Add.Name = i
i = i +5
Loop
and for importing specific individual files, this line also seems to work fine (when including the .FileNames .RowNumbers. RefreshPeriod etc. commands):
With Activesheet.QueryTables.Add(Connection:="TEXT;C:\temp\load_excel\15.txt" _, Destination:=Range ("$A$1"))
I would like to replace the "TEXT;C:\temp\load_excel\15.txt" with something more that allows me to use two different variables to change the files being imported:
Dim Folder As String
Dim File As String
Dim DQ as String
DQ = """" 'double quotation marks
Folder = Cells(14, 2).Value 'cell which states C:\temp\load_excel\
File = DQ & "TEXT;" & Folder & i & ".txt" & DQ
'for i = 15 this gives "TEXT;C:\temp\load_excel\15.txt"
Is there a way to incorporate the two so I can have a loop like this?
Do while i <=k
Worksheets.Add.Name = i
Activesheet.QueryTables.Add(Connection:= File _, Destination:=Range ("$A$1"))
i = i +5
Loop
As far as I can see, this should work, but when I try and run it I get a Run-time error '1004': Application or object-defined error. If anyone could help, it would be greatly appreciated.
EDIT: here is exact code being used
Sub ImportPLEtextFiles()
Dim i As Integer ''initial file name
Dim k As Integer ''final file name
Dim DQ As String '' Double quotation marks
Dim Folder As String
Dim File As String
i = Cells(3, 3).Value
k = Cells(5, 3).Value
DQ = """"
Folder = Cells(14, 2).Value
File = DQ & Folder & i & ".txt" & DQ
Do While i <= k
Worksheets.Add.Name = i
File = DQ & "TEXT;" & Folder & i & ".txt" & DQ
With ActiveSheet.QueryTables.Add(Connection:=File _
, Destination:=Range("$A$1"))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
i = i + 5
Loop
End Sub
Put this inside your loop.
File = "TEXT;" & Cells(14, 2).Value & i & ".txt"
With Sheets(i).QueryTables.Add(Connection:= _
File, Destination:=Range("$A$1"))
.Refresh BackgroundQuery:=False
End With

Resources