import part of text from txt to excel

sai_sl

New Member
Joined
Oct 25, 2021
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone! I need may be some VBA script or something else to solve my problem) I have many txt files in one folder. Each file contain information like:

some text here
some text here
some text here
start to copy
some text here
some text here
some text here
end of copy
some text here
some text here
some text here


So i need to copy/import text include "start to copy" and "end of copy" to one column in excel.

Sceenshot for example in attach.
Screenshot_1.pngScreenshot_2.png

Thanks for your help!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
VBA Code:
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

Download file : Import Txt Files All To Sheet One.xlsm
 
Upvote 0
VBA Code:
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

Download file : Import Txt Files All To Sheet One.xlsm
Thanks, but it's only import txt to excel without filter. I need import only part of text from txt
 
Upvote 0
Will the rows to be copied always be the same ?

OR -

Will the first row in the copy always have the same term , and will there always be the same number of rows after that to be copied ?
 
Upvote 0
Ok, yet one example

This is txt file


1.png


after import to excel, i need this result

2.png


Numbers of rows between vst_start and vst_end may be random
 
Upvote 0
I am at a loss how to assist with these requirements. Another volunteer on the Forum will provide the answer for you.

Thank you for the opportunity.
 
Upvote 0
I have many txt files in one folder
Hello ! What is the End Of Line sequence of your text files ?​
If you do not know so just link a couple of these text files on a files host website like Dropbox …​
Where are located these text files, in same workbook folder or … ?​
As guessing can't be coding …​
 
Upvote 0
Thanks all. My problem was solved.

VBA Code:
Option Explicit
  
Const vst_start = "vst_start"
Const vst_end = "vst_end"
  
Sub ImportTXTFiles()
    Dim importrow As Long
    Dim fso As Object
    Dim xlsheet As Worksheet
    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
        Dim txt As String
        Dim arr As Variant
        Dim brr As Variant
        Dim vv As Variant
        For Each txtfile In txtfilesToOpen
             
            With fso.OpenTextFile(txtfile, 1, False)
                txt = .ReadAll
                .Close
            End With
             
            arr = Split(txt, vst_start)
             
            If Not IsEmpty(arr) Then
                For Each vv In arr
                    If InStr(vv, vst_end) > 0 Then
                        txt = Split(vv, vst_end)(0)
                        txt = Join(Array(vst_start, txt, vst_end), "")
                        brr = Split(txt, vbCrLf)
                         
                        importrow = 1 + .Cells(.Rows.Count, 1).End(xlUp).Row
                        .Cells(importrow, 1).Resize(UBound(brr) + 1, 1) = Application.Transpose(brr)
                    End If
                Next
            End If
        Next txtfile
  
    End With
  
    Application.ScreenUpdating = True
    MsgBox "Successfully imported text files!", vbInformation, "SUCCESSFUL IMPORT"
  
    Set fso = Nothing
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top