none
Trimming strings in a line or per line RRS feed

  • Question

  • I have some issues with trimming strings, but what if you have the following file.

    In "Another List of Names.csv"

    Male Names: Dave, Jack, John Female Names: Cindy, Mary, Pam

    What I want is the following:

    In Text Number One.csv:

    Male Names: Dave, Jack, John

    And in Text Number Two.csv

    Female Names: Cindy, Mary, Pam

    So far, I have the following code snippet:

    Sub AllEntriesToDifferentFiles()
    Dim Xp, NEntry As Long
    Dim strZ, strM, strF, strCropM, strCropF, strTrimStr, strEntrireLine As String

    Open "C:\Users\John\Documents\Another List of Names.csv" For Input As #1
    Open "C:\Users\John\Documents\Text Number One.csv" For Output As #3
    Open "C:\Users\John\Documents\Text Number Two.csv" For Output As #4
    strM = Trim("Male Names")
    strF = Trim("Female Names")
    strCropM = Left(strM, 25)
    strCropF = Left(strF, 25)
    While Not EOF(1)
        Line Input #1, strZ
        If InStr(1, strZ, strM, vbBinaryCompare) Then
            Write #3, strM
        End If
        If InStr(1, strZ, strF, vbBinaryCompare) Then
            Write #4, strF
        End If
    Wend
    Close #4, 3, 1

    End Sub

    So far, it has given the output:

    In Text Number One.csv:

    Male Names

    And in Text Number Two.csv:

    Female Names

    How could I improve this source code snippet, so that it can produce the output that I want?

    JohnDBCTX

    Saturday, June 25, 2016 10:54 PM

All replies

  • See http://www.eileenslounge.com/viewtopic.php?f=30&t=23886

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Saturday, June 25, 2016 11:38 PM
  • Here I have improved the code snippet:
    Sub AllEntriesToDifferentFiles()
    Dim strZ As String, strM As String, strF As String, strCropM As String, strCropF As String

    Open "C:\Users\John\Documents\Another List of Names.csv" For Input As #1
    Open "C:\Users\John\Documents\Text Number One.csv" For Output As #3
    Open "C:\Users\John\Documents\Text Number Two.csv" For Output As #4

    While Not EOF(1)
        Line Input #1, strZ
            strCropM = Left(strZ, 29)
            strCropF = Right(strZ, 30)
        If InStr(1, strZ, "Male Names:", vbBinaryCompare) Then
            Write #3, strCropM
        End If
        If InStr(1, strZ, "Female Names:", vbBinaryCompare) Then
            Write #4, strCropF
        End If
    Wend
    Close #4, 3, 1

    End Sub

    But that might not be enough.  If I am supposed to search the text from left to right per line, how am I supposed to perform that task?
    If [text within search] = "Male Names:"
    then output in Text Number One.txt Male Names:
    Until cursor reaches the text = "Female Names",  and vice versa.

    In this case it is supposed to be commented.

    Hopefully, I do need to be more specific and concise.

    JohnDBCTX


    • Edited by JohnDBCTX Sunday, June 26, 2016 12:37 AM More specific and concise
    Sunday, June 26, 2016 12:35 AM
  • See the thread I mentioned in my previous reply.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, June 26, 2016 9:22 AM