How would I create a loop with Microsoft Visual Basic to import multiple .txt files into excel; creating a seperate worksheet for each .txt file and nameing the worksheet based on the filename? I have hundreds of these .txt files. I would also like to be able to import more of these .txt files into the same workbook in the future.
Thank You
Not tested but maybe:
Sub Import_Text() Dim strFilePath As String Dim strFileName As String Dim wsWriteSheet As Worksheet strFilePath = "C:\Test" strFileName = Dir(strFilePath & "\*.txt") Do While strFileName <> "" Open strFilePath & "\" & strFileName For Input As #1 Set wsWriteSheet = Worksheets.Add(after:=Sheets(Sheets.Count)) With wsWriteSheet .Range("A1") = Input(LOF(1), #1) .Name = strFileName End With Close strFileName = Dir() Loop End Sub
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Thank You I will try it
Each file in a separate worksheet in the same workbook:
Sub tst() c00 = "E:\" c01 = Dir(c00 & "*.txt") Do Until c01 = "" FileCopy c01, Replace(c01, ".txt", ".csv") With GetObject(Replace(c01, ".txt", ".csv")) .Sheets(1).Copy , ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) .Close End With c01 = Dir Loop End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks