-
May 7th, 2013, 03:43 PM
#1
if condition
Hi Guys,
I had a macro which finds the 12th character in between the text from 2nd tab to 3rd tab and find the space “ “ reversely and replace the space “ “ with “~~~”
But if the para doesn’t contain tab, the macro fails to run. The macro stuck and throws the "Subscript out of range" Error
How to fix it? i.e., If the para doesn’t contain tab (if the condition is false) the macro should skip to the 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 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), 12)
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
Next
End With
End Sub
Sample file attached for your reference
Thanks in advance,
Star
-
May 7th, 2013, 07:31 PM
#2
Re: if condition
You need another IF statement to check if there is a tab, then decide what to do.
-
May 9th, 2013, 03:33 PM
#3
Re: if condition
I am zero in programming. I got this code by googling, which is useful to me.
Can anyone help me in this case?
-
May 9th, 2013, 07:48 PM
#4
Re: if condition
Your code should already be moving to the next paragraph if the tab exists or not as the if test is within a for each loop. If the test is true it should so some processing then move on to the next one, if the test is false it should skip the processing and move on to the next one
Always use [code][/code] tags when posting code.
-
May 10th, 2013, 12:09 PM
#5
Re: if condition
Hi DataMiser,
If the para doesn’t contain tab, the macro stuck and throws the "Subscript out of range" Error in the below line.
If Len(Split(Para.Range.Text, vbTab)(2)) > 10 Then
-
May 10th, 2013, 02:19 PM
#6
Re: if condition
I missed the (2) on there. Like said earlier you need another If Test to make sure that there is at least 2 tabs for that to not return an error
Always use [code][/code] tags when posting code.
-
May 10th, 2013, 03:14 PM
#7
Re: if condition
Can any Helping mind write code for me?
-
May 10th, 2013, 06:00 PM
#8
Re: if condition
Code:
If UbOUND(Split(Para.Range.Text, vbTab))>1 Then
If Len(Split(Para.Range.Text, vbTab)(2)) > 10 Then
something like that
Always use [code][/code] tags when posting code.
-
May 10th, 2013, 07:15 PM
#9
Re: if condition
Hi DataMiser,
Its works fine. Thank you very much.
Regards,
Star
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
|