I am trying to automate preparing csv files for import into another program. I have created a button to run a macro to import .csv.
Sub importcsv()
Dim fStr As String
With Application.FileDialog(msoFileDialogFilePicker)
.Show
If .SelectedItems.Count = 0 Then
MsgBox "Cancel Selected"
Exit Sub
End If
fStr = .SelectedItems(1)
End With
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;fStr", _
Destination:=Range("$A$1"))
.Name = "fStr"
.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, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
It lets me chose the file from the browse for location dialog box, but I get an error before the data is loaded.
Run time error '1004':
Excel cannot find the text file to refresh this external data range.
Check to make sure the text file has not been moved or renamed, then try the refresh again.
At this point in the code:
.Refresh BackgroundQuery:=False
End With
End Sub
I am working with Excel 2010 and 2013, if there are conflicts I would rather it to work with Excel 2013
Bookmarks