Split Text with line feed (1 Viewer)

Khalid_Afridi

Registered User.
Local time
Today, 22:33
Joined
Jan 25, 2009
Messages
491
How do you do this?

In a textbox1 if:
1. text length > 150 – including spacing
2. go to the 150th character
3. copy all the text starting from character 1 to 150
4. put them in a variable ‘txtText'
5. start from 151th character in the textbox till next 150th character
6. concatenate them with ‘txtText' with new two lines feed
7. repeat from step 1 till step 6 until last character of textbox1…
8. populate textbox2 with all together i.e after each 150th character there should be 2 line breaks and then start it from next remaining text till the end.

textbox1:
The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog

Textbox2:
The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog

The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog

The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog
The quick brown fox jumps right over the lazy dog

Any idea ;)
 

DCrake

Remembered
Local time
Today, 20:33
Joined
Jun 8, 2005
Messages
8,632
First question WHY? what is your intention, motivation, ideology behind this?
 

Khalid_Afridi

Registered User.
Local time
Today, 22:33
Joined
Jan 25, 2009
Messages
491
Yes David!

Not very special, its just a fun and there is a website where you can send SMS but only for 150 characters. if you have a longer text you have to divide into chunks.

I want it to do it through a code, so the text can be prepared before it send :)
 

DCrake

Remembered
Local time
Today, 20:33
Joined
Jun 8, 2005
Messages
8,632
Well whatever turns you on. but heres how to do it.

Code:
Dim sStrLong As String
Dim sStrWrapped As String

Dim i and Integer

sStrLong = "Your Very Long String Of Text"


   For i = 1 To Len(sStrLong) Step 150
       If Len(Mid(sStrLong,i)) < 150 Then
          sStrWrapped = sStrWrapped & Mid(sStrLong,i)
          Exit For
       End If

        sStrWrapped = sStrWrapped & Mid(sStrLong,i,150) & vbNewLine
       
   Next i
   Debug.Print sStrWrapped

See if you can decipher how it works.:cool:
 

DCrake

Remembered
Local time
Today, 20:33
Joined
Jun 8, 2005
Messages
8,632
The code supplied was all aircode and as such errors and omissions can be expected.

Typo with

Dim i and Integer

Did it give you the desired results? And how do you know I am not a VBA Doctor?

What it does not do is to check that the point of the split is not in the middle of a word. You may want to add that so that it loops at the breakpoint until it hits a space.
 

Khalid_Afridi

Registered User.
Local time
Today, 22:33
Joined
Jan 25, 2009
Messages
491
Did it give you the desired results? And how do you know I am not a VBA Doctor?

Yes David!

I change it a little bit to get my results in a text box, while doing that i made a mistake.
I change this:
For i = 1 To Len(sStrLong) Step 125
If Len(Mid(sStrLong,i)) < 125 Then

and forgot to change here:
sStrWrapped = sStrWrapped & Mid(sStrLong,i,150) & vbNewLine

so I got a confusing results.:eek: The code started to eat my TEXT
soon I realized that here is the mistake, otherwise I was goint to post it back for errors ;)

What it does not do is to check that the point of the split is not in the middle of a word. You may want to add that so that it loops at the breakpoint until it hits a space.
This would be fantastic if you do that.

hehehe you definitely a VBA doctor I believe ;) Administrator should be a doctor in his field.

Thanks again David!
 

DCrake

Remembered
Local time
Today, 20:33
Joined
Jun 8, 2005
Messages
8,632
Hey I showed you how to do it, I'm not here to write it for you. You will never learn without trying thinks for yourself. Relying on other people is not productive.
 

Khalid_Afridi

Registered User.
Local time
Today, 22:33
Joined
Jan 25, 2009
Messages
491
Hey I showed you how to do it, I'm not here to write it for you. You will never learn without trying thinks for yourself. Relying on other people is not productive

Ohhh David!
This is a challenge! Okay never mind ;) I will show you once I'll do it.

Something to do here: (a pseudo code)
Code:
   For i = 1 To Len(sStrLong) Step 150
       If Len(Mid(sStrLong,i)) < 150 Then
          sStrWrapped = sStrWrapped & Mid(sStrLong,i)
          Exit For
       End If
           [COLOR="Red"]If Last character of sStrLong is " "  then
                 sStrWrapped = sStrWrapped & Mid(sStrLong,i,150) & vbNewLine
                 Else
                 len till " " character of sStrLong   
                 sStrWrapped = sStrWrapped & Mid(sStrLong,i,150) & vbNewLine
           End If  [/COLOR]
   Next i

Am I right?
 

Users who are viewing this thread

Top Bottom