Consulting

Results 1 to 7 of 7

Thread: Solved: Looking for a solution

  1. #1
    VBAX Regular
    Joined
    Mar 2005
    Posts
    8
    Location

    Solved: Looking for a solution

    I have a text file full of data. I am trying to find a way to extract a series of
    14 character numbers between exclamation points.

    This is what I have
    sdfhfghjBwBTdNjBwB!12345678901234!BwBTcNjBwBTdN!jBwB!

    I am trying to get just the 12345678901234 out of the file into another text file.
    There are 3014 instances of serial numbers like this !12345678901234! in the database file.
    Is there a way to pull that data and port it to another file, or am I wishful thinking?

  2. #2
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Give this a shot. Relies on the RegExpFind function found here: http://vbaexpress.com/kb/getarticle.php?kb_id=841




    [VBA]
    Sub GrabThem()

    Dim arr As Variant
    Dim fso As Object, ts1 As Object, ts2 As Object
    Dim TheLine As String
    Dim Counter As Long

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts1 = OpenTextFile("c:\folder\subfolder\sourcefile.txt")
    Set ts2 = CreateTextFile("c:\folder\subfolder\resultfile.txt", True)

    Do Until ts1.AtEndOfStream
    TheLine = ts1.ReadLine
    arr = RegExpFind(TheLine, "!\d{14}!")
    If IsArray(arr) Then
    For Counter = 0 To UBound(arr)
    ts2.WriteLine Replace(arr(Counter), "!", "")
    Next
    End If
    Loop

    ts1.Close
    ts2.Close
    Set ts1 = Nothing
    Set ts2 = Nothing
    Set fso = Nothing

    MsgBox "Done"

    End Sub
    [/VBA]
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  3. #3
    VBAX Regular
    Joined
    Mar 2005
    Posts
    8
    Location

    Error

    Hmm...I'm getting a compile error.
    Sub or Function not defined...

    OpenTextFile

  4. #4
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Sorry, change to:

    [VBA]
    Set ts1 = fso.OpenTextFile("c:\folder\subfolder\sourcefile.txt")

    [/VBA]
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  5. #5
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    and the next line to:

    [VBA]
    Set ts2 = fso.CreateTextFile("c:\folder\subfolder\resultfile.txt", True)

    [/VBA]
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  6. #6
    VBAX Regular
    Joined
    Mar 2005
    Posts
    8
    Location

    WOW!

    That works great. Thank you!


  7. #7
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Glad to help. Please remember to mark the thread as solved
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

Posting Permissions

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