ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Open several text files using a macro (https://www.excelbanter.com/excel-discussion-misc-queries/116781-open-several-text-files-using-macro.html)

Jade5

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






All times are GMT +1. The time now is 02:20 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com