Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split A Big String Into Multiple Lines (Max Lines Predefined Defined)

Status
Not open for further replies.

sam4help

Programmer
Jul 22, 2011
74
AE
Hello All,

I trying a Function to Validate a Field data before passing it to the Process.

Validation Details Example;
Field1 = Validation [Max 20 Characters Per Line x 1 Line]
Field2 = Validation [Max 15 Characters Per Line x 3 Lines]

Requirement Details:
StringExample : Validation [Max 20 Characters Per Line x 5 Lines]
StringExample = "UNITED KINGDOM PUMPING SOLUTIONS MANUFACTURING AND ENGINEERING SERVICING COMPANY LIMITED, LONDON"

It should return me the spitted data in a string with vbCr at each line break as output string


Thanking you in advance for your Inputs,

Sam
 

Is this what you are hoping to get:
[tt]
UNITED KINGDOM PUMPI
NG SOLUTIONS MANUFAC
TURING AND ENGINEERI
NG SERVICING COMPANY
LIMITED, LONDON[/tt]

???
Max 20 Characters Per Line x 5 Lines

Have fun.

---- Andy
 
Hi Sam,

I assume you do not want to wrap in the middle of words?
In that case you first have to split the string into words, then re-concatenate these words (with a space) until they hit max length.

I have put the following together in Word VBA but you can easily adapt it to your own needs.
Code:
Sub Split2Twenty()
Dim StringExample As String
Dim Fragments As Variant
Dim i As Integer
Dim TheLine As String, MaxLen As Integer

StringExample = "UNITED KINGDOM PUMPING SOLUTIONS MANUFACTURING AND ENGINEERING SERVICING COMPANY LIMITED, LONDON"

Fragments = Split(StringExample, " ")
MaxLen = 20

TheLine = ""

Do While i <= UBound(Fragments)
    Do While (Len(TheLine) + Len(Fragments(i)) <= MaxLen)
        TheLine = TheLine & Fragments(i) & " "
        i = i + 1
        If i > UBound(Fragments) Then Exit Do
    Loop
    TheLine = RTrim(TheLine)
    Selection.TypeText TheLine & vbCr
    TheLine = ""
Loop

End Sub

You'll have to replace the "Selection.TypeText" with your respective output proc of course.
This also does not take into account a max number of lines.

Should help you get going though.

Cheers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Thanks MakeItSo and Andy,

I was hoping for complete words and if its cutting then next line.
I have got it done. Thanks Again,

Best Regards,

Sam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top