PDA

View Full Version : Adding Line breaks in LONG string of data



GBraniger
02-25-2015, 03:28 AM
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

Paul_Hossler
02-26-2015, 08:20 AM
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