+ Reply to Thread
Results 1 to 4 of 4

Thread: Copy text file after "phrase" in the text

  1. #1
    Registered User
    Join Date
    04-27-2011
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    10

    Copy text file after "phrase" in the text

    G'day
    I have a text file that I need to import into Excel. The file is generally about 100,000 lines but I only need the data after the phrase "Adjusted Coordinates". The location of the phrase changes within the text file as more data is added from the program that created it. It is unfortunately not a unique phrase as it is repeated a second time 3 lines later.

    I have had a crack at getting this to work but I currently have to manually delete all of the data above "Adjusted Coordinates" and then import it (see below).

    I would prefer if the required data can just be extracted from the text file without altering the original file.

    Attached is an example of the text file. I have taken a lot of the data out of it to make it a smaller file but I think the same principle should apply.

    Thanks in advance

    Sub Import_old()
    
    'Opens the the Dialog box to select the file
        Dim lCount As Long
        Dim GetFile As String
        With Application.FileDialog(msoFileDialogOpen)
            .Show
            For lCount = 1 To .SelectedItems.Count
                GetFile = .SelectedItems(lCount)
            Next lCount
        End With
        
    'Inserts the selected file into excel
        Dim strFile As String
        strFile = GetFile
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & GetFile, _
            Destination:=Range("A1"))
            .Name = GetFile
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = True
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = True
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Range("E5:P5").Select
        Selection.Cut Destination:=Range("F5:Q5")
        Range("F5:Q5").Select
        ActiveWindow.ScrollColumn = 4
        ActiveWindow.ScrollColumn = 3
        ActiveWindow.ScrollColumn = 2
        ActiveWindow.ScrollColumn = 1
        Range("D4:H4").Select
        Selection.ClearContents
    End Sub
    Attached Files Attached Files
    Last edited by fredo; 04-29-2011 at 12:52 AM. Reason: Added Code Tags

  2. #2
    Forum Guru
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2003
    Posts
    1,241

    Re: Copy text file after "phrase" in the text

    You could use this macro that creates 'temp.txt' file in the same path of your selected file and then delete it after import:

    Sub Import_old()
    
    'Opens the the Dialog box to select the file
        Dim lCount As Long
        Dim GetFile As String
        With Application.FileDialog(msoFileDialogOpen)
            .Show
            For lCount = 1 To .SelectedItems.Count
                GetFile = .SelectedItems(lCount)
            Next lCount
        End With
        
      
    'Extract data from Adjusted Coordinates and write it on temp file
        Dim objFSO As Object, fso As Object
        Dim tempFileName As String, myRec As String
        Dim swDelete As Boolean
        
        tempFileName = Left(GetFile, InStrRev(GetFile, "\")) & "temp.txt"
        swDelete = True
        
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set fso = objFSO.OpenTextFile(GetFile)
        myRec = fso.Readall
        fso.Close
    
        p = InStr(myRec, "Adjusted Coordinates")
        If p = 0 Then
            tempFileName = GetFile
            swDelete = False
        Else
            Set fso = objFSO.OpenTextFile(tempFileName, 2, True)
            fso.Write Mid(myRec, p)
            fso.Close
        End If
        
    'Inserts the selected file into excel
        Dim strFile As String
        strFile = GetFile
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & tempFileName, _
            Destination:=Range("A1"))
            .Name = GetFile
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = True
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = True
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Range("E5:P5").Select
        Selection.Cut Destination:=Range("F5:Q5")
        Range("F5:Q5").Select
        ActiveWindow.ScrollColumn = 4
        ActiveWindow.ScrollColumn = 3
        ActiveWindow.ScrollColumn = 2
        ActiveWindow.ScrollColumn = 1
        Range("D4:H4").Select
        Selection.ClearContents
    
    'Delete temp file
       If swDelete Then
          Set fso = objFSO.GetFile(tempFileName)
          fso.Delete
       End If
    End Sub
    Regards,
    Antonio

  3. #3
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,154

    Re: Copy text file after "phrase" in the text

    Hi fredo,
    or maybe something like
    Option Explicit
    Sub FilterFileTwo()
        On Error Resume Next
       Dim XBoolean As Boolean
       Dim data As String
       Dim xRow As Long
        XBoolean = False
        Open ThisWorkbook.Path & "\Example.txt" For Input As #1
        If Err <> 0 Then
            MsgBox "Error reading or writing a file."
            Exit Sub
        End If
        Do While Not EOF(1)
            Line Input #1, data
            If InStr(1, data, "Adjusted Coordinates") Then XBoolean = True
            If InStr(1, data, "---") Then XBoolean = False
            If XBoolean Then
             xRow = xRow + 1
             Cells(xRow, 1) = data
            End If
        Loop
     Close #1
    Call MacroTexttoCloumn
    End Sub
    Sub MacroTexttoCloumn()
       With Columns("A:A")
        .TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
            FieldInfo:=Array(Array(0, 1), Array(9, 1), Array(16, 1), Array(27, 1), Array(40, 1), _
            Array(43, 1), Array(54, 1), Array(69, 1), Array(83, 1), Array(88, 1), Array(95, 1), Array( _
            104, 1), Array(109, 1), Array(115, 1), Array(122, 1), Array(129, 1), Array(136, 1)), _
            TrailingMinusNumbers:=True
       End With
    End Sub
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  4. #4
    Registered User
    Join Date
    04-27-2011
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Copy text file after "phrase" in the text

    Thankyou both for you help.

    I went with the one Antoka05 suggested as I understang what is happening in each step (still very new to VBA).

    I have tried it with the large file (100,000 lines) and it works great.

    Thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0