vincentzack
New Member
- Joined
- Jul 2, 2016
- Messages
- 8
I need to import multiple text files (all the same format) into an excel spreadsheet. The text files are saved in a folder, e.g. C:\Test. The path can be changed in the excel (like the attached excel, worksheet("Master"), cells (B1))
I want to import some of the text files from the folder (not import all) to separate worksheet. For example: import BEAMA.txt, then excel will have a sheet called BEAMA".
I only have the code to import the text file one by one. However, I need to import 50 nos. of text file out of 200. Could anyone help?
I want to import some of the text files from the folder (not import all) to separate worksheet. For example: import BEAMA.txt, then excel will have a sheet called BEAMA".
I only have the code to import the text file one by one. However, I need to import 50 nos. of text file out of 200. Could anyone help?
Code:
[/COLOR]
[COLOR=#333333]Sub ImportData()[/COLOR]
[COLOR=#333333]Dim txtFileNameAndPath As String[/COLOR]
[COLOR=#333333]Dim ImportingFileName As String, ImportingFileName2 As String[/COLOR]
[COLOR=#333333]Dim SheetName As Worksheet[/COLOR]
[COLOR=#333333]Dim fd As Office.FileDialog[/COLOR]
[COLOR=#333333]Set fd = Application.FileDialog(msoFileDialogFilePicker)[/COLOR]
[COLOR=#333333]With fd[/COLOR]
[COLOR=#333333]'Enable this option if you want the use to be able to select multiple files[/COLOR]
[COLOR=#333333].AllowMultiSelect = False[/COLOR]
[COLOR=#333333]'This sets the title of the dialog box.[/COLOR]
[COLOR=#333333].Title = "Please select the file."[/COLOR]
[COLOR=#333333]'Sets the associated filters for types of files[/COLOR]
[COLOR=#333333].Filters.Clear[/COLOR]
[COLOR=#333333].Filters.Add "txt", "*.txt"[/COLOR]
[COLOR=#333333].Filters.Add "All Files", "*.*"[/COLOR]
[COLOR=#333333]' Show the dialog box. If the .Show method returns True, the[/COLOR]
[COLOR=#333333]' user picked at least one file. If the .Show method returns[/COLOR]
[COLOR=#333333]' False, the user clicked Cancel.[/COLOR]
[COLOR=#333333]If .Show = True Then[/COLOR]
[COLOR=#333333]txtFileNameAndPath = .SelectedItems(1)[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]MsgBox "Please start over. You must select a file to import"[/COLOR]
[COLOR=#333333]'You don't want the sub continuing if there wasn't a file selected[/COLOR]
[COLOR=#333333]Exit Sub[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]'Extracts only the file name for reference later[/COLOR]
[COLOR=#333333]ImportingFileName = Right(txtFileNameAndPath, _[/COLOR]
[COLOR=#333333]Len(txtFileNameAndPath) - InStrRev(txtFileNameAndPath, "\"))[/COLOR]
[COLOR=#333333]'Nneed to be on the active worksheet for the below code to work[/COLOR]
[COLOR=#333333]ImportingFileName2 = Left(ImportingFileName, Len(ImportingFileName) - 4)[/COLOR]
[COLOR=#333333]ThisWorkbook.Sheets.Add.Name = ImportingFileName2[/COLOR]
[COLOR=#333333]Worksheets(ImportingFileName2).Activate[/COLOR]
[COLOR=#333333]With ActiveSheet.QueryTables.Add(Connection:= _[/COLOR]
[COLOR=#333333]"TEXT;" & txtFileNameAndPath _[/COLOR]
[COLOR=#333333], Destination:=Worksheets(ImportingFileName2).Range("$A2"))[/COLOR]
[COLOR=#333333].Name = "ImportingFileName"[/COLOR]
[COLOR=#333333].FieldNames = True[/COLOR]
[COLOR=#333333].RowNumbers = False[/COLOR]
[COLOR=#333333].FillAdjacentFormulas = False[/COLOR]
[COLOR=#333333].PreserveFormatting = True[/COLOR]
[COLOR=#333333].RefreshOnFileOpen = False[/COLOR]
[COLOR=#333333].RefreshStyle = xlInsertDeleteCells[/COLOR]
[COLOR=#333333].SavePassword = False[/COLOR]
[COLOR=#333333].SaveData = True[/COLOR]
[COLOR=#333333].AdjustColumnWidth = True[/COLOR]
[COLOR=#333333].RefreshPeriod = 0[/COLOR]
[COLOR=#333333].TextFilePromptOnRefresh = False[/COLOR]
[COLOR=#333333].TextFilePlatform = 950[/COLOR]
[COLOR=#333333].TextFileStartRow = 1[/COLOR]
[COLOR=#333333].TextFileParseType = xlDelimited[/COLOR]
[COLOR=#333333].TextFileTextQualifier = xlTextQualifierDoubleQuote[/COLOR]
[COLOR=#333333].TextFileConsecutiveDelimiter = False[/COLOR]
[COLOR=#333333].TextFileTabDelimiter = True[/COLOR]
[COLOR=#333333].TextFileSemicolonDelimiter = False[/COLOR]
[COLOR=#333333].TextFileCommaDelimiter = False[/COLOR]
[COLOR=#333333].TextFileSpaceDelimiter = False[/COLOR]
[COLOR=#333333].TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1)[/COLOR]
[COLOR=#333333].TextFileTrailingMinusNumbers = True[/COLOR]
[COLOR=#333333].Refresh BackgroundQuery:=False[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]End Sub[/COLOR][COLOR=#333333]