Results 1 to 2 of 2

Thread: VBA question..

  1. #1
    Join Date
    Feb 2009
    Posts
    35

    Unanswered: VBA question..

    Hi

    I was wondering if somebody can help or at least point me in the right direction..
    I have a txt input file and am trying to export specific data from this file into an output txt file.

    The input file looks something like this :

    CPNTCS
    LIN01
    LIN02
    LIN03
    CPNTCS
    LIN01
    LIN02
    CPNPTC
    LIN01
    CPNLPL
    LIN01
    LIN02

    For the output, I need the file to output ONLY the "CPNTCS" with their corresponding lines (to skip all other records.)

    I have attached the sample code I am using - this basically outputs the CPNTCS lines but no other info. See below :

    Option Compare Database
    Option Explicit

    Function OutputTCS(InFile As String, OutFile As String)

    Dim intInHandle As Integer
    Dim intOutHandle As Integer
    Dim strInLine As String
    Dim strOutLine As String

    intInHandle = FreeFile
    Open InFile For Input As #intInHandle
    intOutHandle = FreeFile
    Open OutFile For Output As #intOutHandle

    Do Until EOF(intInHandle)
    Line Input #intInHandle, strInLine
    If Left(strInLine, 6) = "CPNTCS" Then
    strOutLine = strInLine
    End If
    Print #intOutHandle, strOutLine
    Loop

    End Function


    How can this be done? or what statement would be required.

    Thanks

  2. #2
    Join Date
    May 2009
    Posts
    258
    Hello st3ven_J,

    Sorry for the late response. If every header line begins with "CPN", you can change your Do loop to the following:
    Code:
    Do Until EOF(intInHandle)
    	Line Input #intInHandle, strInLine
    	If Left(strInLine, 3) = "CPN" Then
    		strHeader = Left(strInLine, 6)
    	End If
    	If strHeader = CPNTCS" Then
    		Print #intOutHandle, strInLine
    	End If
    Loop
    You'll need a new strHeader variable and will no longer need the strOutLine variable.

    Regards,

    Ax

Posting Permissions

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