Consulting

Results 1 to 2 of 2

Thread: Adding Line breaks in LONG string of data

  1. #1

    Adding Line breaks in LONG string of data

    I have a very long string of data in a word document that I need to add line breaks to make it more readable. The string follows this criteria:

    The data starts as one long string with a small header (below would be ABC110) and then will always have a H001 to follow. Then moves based on the value following the 001. So for H001 it is 26 bytes long, C001 it is 29 bytes long, S001 is 21 bytes long, and A001 is 9 bytes long. There is always a space after the length.

    ABC110H00126 several bytes laterC00129 Another length of dataS00121 more data hereS00121 more data hereA0019 end

    The above data would end up looking like:

    ABC110
    H00126 several bytes later
    C00129 Another length of data
    S00121 more data here
    S00121 more data here
    A0019 end

    I would really like to automate this formatting considering my data is normally around 25,000 bytes long and would break down to about 150 lines on average. Doing this 10 to 20 times a day is just taking too much time!

    Thanks for the help!
    Greg

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I'd do something like this -- assumes a lot about the format, but that can be generalized if needed

    I wasn't sure what the doc looked like, but the logic to split the long string seems to be Ok.

    e.g I didn't know if the doc was one long string in a single para and you wanted to split that into lots of short paragraphs



    Option Explicit
    
    'The data starts as one long string with a small header (below would be ABC110)
    'and then will always have a H001 to follow.
    'Then moves based on the value following the 001.
    'So for H001 it is 26 bytes long, C001 it is 29 bytes long,
    'S001 is 21 bytes long, and
    'A001 is 9 bytes long.
    'There is always a space after the length.
    
    Sub breaks()
        Dim sIn As String, sWork As String, sOut As String
        Dim iLen As Long
        
        sIn = "ABC110H00126 several bytes laterC00129 Another length of dataS00121 more data hereS00121 more data hereA0019 end"
        
        sWork = sIn
        iLen = -1
        
        
        'header assumes header is any length
        On Error Resume Next
        iLen = InStr(sWork, "00")    ' first 0 of 00
        On Error GoTo 0
        If iLen = 0 Then Exit Sub     ' if no 00
        
        iLen = iLen - 2               '   char before H00
        sOut = Left(sWork, iLen) & vbCrLf
        sWork = Right(sWork, Len(sWork) - iLen)
        
        
        'data assume format always X000nn where len of data = nn
        Do While Len(sWork) > 0
        
            iLen = CLng(Mid(sWork, 5, 2)) ' the 26 in H00126
            sOut = sOut & Left(sWork, iLen) & vbCrLf
            sWork = Right(sWork, Len(sWork) - iLen)
        Loop
        MsgBox sOut
    
    End Sub



    This is an attempt to do it within a word doc. Select the long text and run the macro and see if it close for you


    Option Explicit
    Sub breaks_doc()
        Dim sIn As String, sWork As String
        Dim iLen As Long
        
        sIn = Selection.Text
        Selection.Delete
        
        sWork = sIn
        iLen = -1
        
        
        'header assumes header is any length
        On Error Resume Next
        iLen = InStr(sWork, "00")    ' first 0 of 00
        On Error GoTo 0
        If iLen = 0 Then Exit Sub     ' if no 00
        
        iLen = iLen - 2               '   char before H00
        
        Selection.TypeParagraph
        Selection.TypeText Text:=Left(sWork, iLen)
        Selection.TypeParagraph
        sWork = Right(sWork, Len(sWork) - iLen)
        
        
        'data assume format always X000nn where len of data = nn
        Do While Len(sWork) > 1
        
            iLen = CLng(Mid(sWork, 5, 2)) ' the 26 in H00126
            Selection.TypeText Text:=Left(sWork, iLen)
            Selection.TypeParagraph
            sWork = Right(sWork, Len(sWork) - iLen)
        Loop
    End Sub
    Last edited by Paul_Hossler; 02-26-2015 at 08:54 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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