stuart_robets
New Member
- Joined
- Oct 29, 2016
- Messages
- 3
Hi
I am trying to create a macro to allow a user to select a text file and import it into a sheet in the same workbook, but i just cant understand why this code i have is not working, please could someone point me in the right direct as to where im going wrong.
I am trying to create a macro to allow a user to select a text file and import it into a sheet in the same workbook, but i just cant understand why this code i have is not working, please could someone point me in the right direct as to where im going wrong.
Code:
Sub SelectFile()
Dim fName As String
fName = Application.GetOpenFilename("Text Files (*.txt), *.txt", , "Please Select The File to Import...")
If fName = "False" Then Exit Sub
Sheets("Import sheet").Select
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:="TEXT; " & fName, Destination:=Range("A1"))
.Name = Dir(fName)
.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 = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub