Hello,
I'm trying to import data from delimited text files without Excel saving the query or data connection. I used the Macro Recorder during the manual import, and unchecked the default "Save query definition" and selected "overwrite..." from the Import Data --Properties--External Data Range Properties dialog box:
Capture.jpg
VB:
Sub ImportDataWithUI()
' ############## BEGIN DATA IMPORT ##################
' Import overwrites cell range without inserting new columns
'Get the file name with a UI from
'Walkenbach's Sub GetImportFileName() page 410
Dim Filt As String
Dim FilterIndex As Integer
Dim Title As String
Dim FileName As Variant
' Set up list of file filters
Filt = "Text Files (*.txt),*.txt,(*.csv),*.csv,(*.dat),*.dat," & _
"All Files (*.*),*.*"
' Display *.* by default
FilterIndex = 1
' Set the dialog box caption
Title = "Select a File to Import"
' Get the file name
FileName = Application.GetOpenFilename _
(FileFilter:=Filt, _
FilterIndex:=FilterIndex, _
Title:=Title)
' Exit if dialog box canceled
If FileName = False Then
MsgBox "No file was selected."
Exit Sub
End If
' Display full path and name of the file
' MsgBox "You selected " & FileName
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & FileName _
, Destination:=Range("$A$1"))
.Name = FileName
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = False
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.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
End Sub
If you like these VB formatting tags please consider sponsoring me in support of injured Royal Marines
Unfortunately, the code recorded fails to remove the query definition. So I searched around an found two subs that should do the trick, but don't:
VB:
' From: http://www.mrexcel.com/forum/showthread.php?t=381140
Dim Sh As Worksheet, xNazwa As Object
Dim xConect As Object
For Each xConect In ActiveWorkbook.Connections
If UCase(xConect.Name) Like "*" Then xConect.delete
Next xConect
For Each Sh In ActiveWorkbook.Worksheets
For Each xNazwa In Sh.Names
xNazwa.delete
Next xNazwa
Next Sh
'From http://www.ozgrid.com/forum/showthread.php?t=63309
Dim ws As Worksheet
Dim qt As QueryTable
For Each ws In ThisWorkbook.Worksheets
For Each qt In ws.QueryTables
qt.delete
Next qt
Next ws
End Sub
If you like these VB formatting tags please consider sponsoring me in support of injured Royal Marines
You see, I need to make a table from this data (bogus example file is attached) and if there is a data connection or a query, Excel throws a runtime error 1004 (table cannot intersect query). Once I get the data in, I run the following code to get the size of the data range (where the header row is, where the data rows start and end, and the last column). This code works until I turn it into a table, where things have ground to a halt. Here's the last part of the code:
VB:
Dim DataCellStart As Range
Dim DataHeaderRow As Integer
Dim DataRowStart As Integer 'for starting data calculations
Dim DataRowEnd As Long
Dim DataColEnd As Integer 'this is the last column of imported data
Application.GoTo Reference:="R1C1"
Selection.End(xlDown).Select
Selection.End(xlDown).Select
DataHeaderRow = Selection.Row
Selection.End(xlDown).Select
DataRowEnd = Selection.Row
Selection.End(xlToRight).Select
DataColEnd = Selection.Column
DataRowStart = DataHeaderRow + 1
Range("E1") = "Data Header Row"
Range("E2") = DataHeaderRow
Range("E3") = "Last Data Row"
Range("E4") = DataRowEnd
Range("E5") = "Last Data Column"
Range("E6") = DataColEnd
'Automatic sizing of column widths, selecting like ctrl+shift+*
ActiveCell.CurrentRegion.Select
'Auto-size columns
Selection.Columns.AutoFit
'create a table from the imported data and give it a name
ActiveSheet.ListObjects.Add(xlSrcRange, , xlYes).Name = "ImportedData"
If you like these VB formatting tags please consider sponsoring me in support of injured Royal Marines
There was only one post in ozgrid that I found with the same problem but there was no solution:
http://www.ozgrid.com/forum/showthre...ht=import+text
I'll be grateful for any suggestions. I hope the explanation and breakdown is clear.