Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel macro text import file selection

Status
Not open for further replies.

Ennes

Technical User
Feb 13, 2003
2
US
Hello all,
I have this macro to import a text file in to Excel. How can I modify it so it will ask me which file to import rather than the "dataone.txt". I would also like to rename the worksheet to the name of the text file (ex. dataone).

Thanks in advance.

Here it is:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 2/13/2003 by XXX
'

'
ActiveWorkbook.Worksheets.Add
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;c:\dataone.txt", Destination:=Range _
("A1"))
.Name = "dataone"
.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 = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileOtherDelimiter = "="
.TextFileColumnDataTypes = Array(9, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
 
Try this on for size
Sub opentextfile()
Dim mybook
Set mybook = ThisWorkbook
Application.DisplayAlerts = False

'
filetoopen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If filetoopen <> False Then

Workbooks.OpenText Filename:=filetoopen, Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier _
:=xlNone, ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, _
Comma:=false, Space:=true, Other:=false, FieldInfo:=Array(1, 1)
Range(&quot;A1&quot;).Select

End If
Application.DisplayAlerts = True
Cells.Select
Selection.Copy
mybook.Activate
Cells(1, 1).Select
ActiveSheet.Paste
'Workbooks(filetoopen).Close SaveChanges:=False
End Sub


Andrew299
 
Ennes,

Use the 'InputBox' function. The help pages can guide you through how to implement in your macro...

MM
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top