+ Reply to Thread
Results 1 to 5 of 5

Excel VBA code importing from text file but between certain key words

  1. #1
    Registered User
    Join Date
    02-07-2016
    Location
    Manchester
    MS-Off Ver
    2010
    Posts
    3

    Excel VBA code importing from text file but between certain key words

    What I am trying to do is to import from a text file into excel, yet I only want the data from lines between certain key words.

    For example below is an extract of the text file.

    Please Login or Register  to view this content.
    I only need the data between the line starting with "Date" and the line starting with ">LOGOUT"

    The contents of the text file repeats itself as it connects to different systems but I only require the information that's is between the two key words so to speak.

    I can get the entire text file inputted but really only require that information between the lines.

    Any help is greatly appreciated (non programmer)

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Excel VBA code importing from text file but between certain key words

    If you have only one set of data within a txt file then, try
    PHP Code: 
    Sub test()
        
    Dim fn As Stringtxt As Stringxy
        
    fn = Application.GetOpenFilename("TextFiles,*.txt")
        If fn = 
    "False" Then Exit Sub
        txt 
    CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
        x 
    InStr(1txt">DATE"1)
        
    InStr(1txt">LOGOUT"1)
        If (
    0) * (xThen
            txt 
    Mid$(txtxLen(">LOGOUT") + 1)
        Else
            
    txt "Something is wrong"
        
    End If
        
    MsgBox txt
    End Sub 

  3. #3
    Registered User
    Join Date
    02-07-2016
    Location
    Manchester
    MS-Off Ver
    2010
    Posts
    3

    Re: Excel VBA code importing from text file but between certain key words

    The text file contains multiple instances of the >DATE / >LOGOUT, I just want to capture the info between those markers so to speak, and have it inputted into an excel sheet.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Excel VBA code importing from text file but between certain key words

    Then
    PHP Code: 
    Sub test()
        
    Dim fn As Stringtxt As StringAs Objecttemp As Stringx
        
    fn = Application.GetOpenFilename("TextFiles,*.txt")
        If fn = 
    "False" Then Exit Sub
        txt 
    CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
        With CreateObject
    ("VBScript.RegExp")
            .Global = 
    True
            
    .Pattern "(>DATE)(.*\r\n(?!\1))+>LOGOUT"
            
    For Each m In .Execute(txt)
                
    temp temp vbCrLf m.Value
            Next
        End With
        
    If Len(tempThen
            x 
    Split(Mid$(tempLen(vbCrLf) + 1), vbCrLf)
            
    Cells(1).Resize(UBound(x) + 1).Value Application.Transpose(x)
        Else
            
    MsgBox "No match"
        
    End If
    End Sub 

  5. #5
    Registered User
    Join Date
    02-07-2016
    Location
    Manchester
    MS-Off Ver
    2010
    Posts
    3

    Re: Excel VBA code importing from text file but between certain key words

    Thank you so much that worked perfectly

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA code - importing text file to excel - variable filename!
    By 2010snowbeach in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-10-2014, 01:16 AM
  2. [SOLVED] importing text file into excel file using macro
    By ananthbulusu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-26-2013, 04:08 PM
  3. Import text file into Excel and using a loop to count unique words within the text
    By mrgriff21 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-24-2013, 03:17 PM
  4. Importing a text file into Excel
    By JibrilBenes in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 02-18-2009, 05:31 PM
  5. Importing text file into Excel
    By steadfast199 in forum Excel General
    Replies: 1
    Last Post: 01-27-2009, 01:16 PM
  6. Importing text file to Excel
    By zodiac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-25-2006, 10:23 AM
  7. [SOLVED] Importing Text File into Excel
    By Martin in forum Excel General
    Replies: 1
    Last Post: 04-02-2006, 09:55 AM
  8. [SOLVED] Importing text file to excel
    By dany04 in forum Excel General
    Replies: 1
    Last Post: 11-08-2005, 09:15 PM

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.6.0 RC 1