-
December 2nd, 2014, 02:50 PM
#1
Need some changes in macro
Hi Guys,
I got the below code from net which works fine.
The macro works as follows.
1. Split the para from the 2nd Tab to 3rd Tab.
2. Move the cursor to the 10th character.
3. Find the space reversely and Inserts "~~~"
But it do the process only once. How to loop until the condition fails.
For example:
If the split string contains 42 characters it should loop for 4 times. And then to next para....
Code:
Sub Demo()
Dim Para As Paragraph, Rng As Range, i As Long
Dim StrOut As String, StrTmp As String
With ActiveDocument
For Each Para In .Paragraphs
If UBound(Split(Para.Range.Text, vbTab)) > 1 Then
If Len(Split(Para.Range.Text, vbTab)(2)) > 10 Then
StrOut = ""
Set Rng = Para.Range
Rng.End = Rng.End - 1
For i = 0 To UBound(Split(Rng, vbTab))
If i <> 2 Then
StrOut = StrOut & Split(Rng, vbTab)(i) & vbTab
Else
StrTmp = Left(Split(Rng, vbTab)(i), 10)
StrTmp = Left(StrTmp, InStrRev(StrTmp, " ") - 1) & "~~~"
StrTmp = StrTmp & Mid(Split(Rng, vbTab)(i), Len(StrTmp) - 1, Len(Split(Rng, vbTab)(i)))
StrOut = StrOut & StrTmp & vbTab
End If
Next
Rng.Text = Left(StrOut, Len(StrOut) - 1)
End If
End If
Next
End With
End Sub
-
December 5th, 2014, 12:59 PM
#2
Re: Need some changes in macro
Show a typical string that you are reading. You'll need to determine IF there are 42 characters, then INSERT a split character, and try it again. Should be easier.
-
December 8th, 2014, 10:24 AM
#3
Re: Need some changes in macro
For an example only I have mentioned 42 characters. It may me any amount of characters.
-
December 10th, 2014, 07:23 PM
#4
Re: Need some changes in macro
You say you want to loop until the condition fails. What condition would that be?
Right now you have two loops one looping on paragraphs and one looping on elements of an array.
Always use [code][/code] tags when posting code.
-
December 17th, 2014, 10:42 AM
#5
Re: Need some changes in macro
I want to loop in between the split string. i.e., Text from 2nd tab to 3rd tab
-
December 17th, 2014, 11:50 AM
#6
Re: Need some changes in macro
I'm still not sure what condition you are talking about.
After looking at your code again I can see that the way you are using split() is not good.
Rather than using split() multiple times in the for next loop you should be doing it once above the loop and storing that in an array then using the array for the stuff in the loop. Much more efficient that way, faster and may help in solving your problem.
Always use [code][/code] tags when posting code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|