PDA

View Full Version : Extracting specific lines from multiple text files and save it in a single excel shee



swatian88
12-27-2020, 03:11 AM
Dears,
I have multiple text files in a single folder whereas from each text file i want to extract the contents from line 300 to 600 and save the output of all text files (line 300 to 600) in a single excel sheet.
Appreciate if you can share vba code for the same. I have no experience of vba and looking forward to your support in this regard.

snb
12-27-2020, 03:38 AM
I have no experience of vba

In that case you'd better hire someone who has.

Logit
12-28-2020, 11:56 AM
Option Explicit


Sub ImportTXTFiles()
Dim importrow As Long
Dim fso As Object
Dim xlsheet As Worksheet
Dim qt As QueryTable
Dim txtfilesToOpen As Variant, txtfile As Variant


Application.ScreenUpdating = False
Set fso = CreateObject("Scripting.FileSystemObject")


txtfilesToOpen = Application.GetOpenFilename _
(FileFilter:="Text Files (*.txt), *.txt", _
MultiSelect:=True, Title:="Text Files to Open")


With ActiveSheet


For Each txtfile In txtfilesToOpen


importrow = 1 + .Cells(.Rows.Count, 1).End(xlUp).Row

With .QueryTables.Add(Connection:="TEXT;" & txtfile, _
Destination:=.Cells(importrow, 1))
.TextFileParseType = xlDelimited
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.Refresh BackgroundQuery:=False
End With




Next txtfile


For Each qt In .QueryTables
qt.Delete
Next qt


End With


Application.ScreenUpdating = True
MsgBox "Successfully imported text files!", vbInformation, "SUCCESSFUL IMPORT"


Set fso = Nothing
End Sub




This will get you started.