Open several text files using a macro
Hello:
I have a folder that contains several text files (.txt). I want to open them
all at the same time usng a macro. If my code looks something like this to
open one file, how can I use this macro to open several files in one click. I
would create a button and write the code behind the button. Thanks, Jade
Code
Sub Test_Macro()
Dim myFileName As Variant
fileName = Application.GetOpenFilename(filefilter:="Text Files, *.Txt", _
Title:="Choose File")
If myFileName = False Then
MsgBox "Cancelled"
End If
'the next couple of lines don't work
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Documents and Settings\JGraham\Desktop\" & fileName, _
Destination:=Range("A1"))
.Name = fileName
.FieldNames = True
.RowNumbers = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2)
.Refresh BackgroundQuery:=False
End With
End Sub
|