if condition
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: if condition

  1. #1
    Join Date
    Mar 2013
    Posts
    7

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: if condition

    You need another IF statement to check if there is a tab, then decide what to do.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Mar 2013
    Posts
    7

    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?

  4. #4
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,830

    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.

  5. #5
    Join Date
    Mar 2013
    Posts
    7

    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

  6. #6
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,830

    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.

  7. #7
    Join Date
    Mar 2013
    Posts
    7

    Re: if condition

    Can any Helping mind write code for me?

  8. #8
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,830

    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.

  9. #9
    Join Date
    Mar 2013
    Posts
    7

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center