none
Breaking out of a loop early RRS feed

  • Question

  • Hello again.

    This time, I might receive a wider pool of responses within this forum.

    Here I have the following code snippet:

    Dim Ctr As Integer Dim strX(16) As String Sub modTwo() With Word.Selection Ctr = 1 Do strX(Ctr) = "asdfasdfXasdfasdf" .TypeText strX(Ctr) If InStr(Ctr, strX(Ctr), "X", vbTextCompare) > 0 Then Exit Do End End If Ctr = Ctr + 1 Loop Until Ctr = 16 End With End Sub

    This generates the output:

    asdfasdfXasdfasdf

    What I wanted the program is to break out of the loop when it reads X within the string.

    like this:

    asdfasdfX

    Regards,

    JohnDBCTX


    jp

    Friday, November 25, 2016 10:10 PM

Answers

  • Your code makes no sense. It initializes strX(1) to "asdfasdfXasdfasdf" and then, before testing anything, changes the selection in the document to this string.

    Perhaps you meant this:

    Sub modTwo()
        Dim strText As String
        Dim lngPos As Long
        strText = "asdfasdfXasdfasdf"
        lngPos = InStr(1, strText, "X", vbTextCompare)
        If lngPos > 0 Then
            strText = Left(strText, lngPos - 1)
        End If
        Selection.TypeText strText
    End Sub

    Or, since the value "asdfasdfXasdfasdf" is fixed, you might as well use

    Sub modTwo()
        Selection.TypeText "asdfasdf"
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by JohnDBCTX Friday, November 25, 2016 11:06 PM
    Friday, November 25, 2016 10:51 PM
  • Yes, indeed.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by JohnDBCTX Wednesday, December 14, 2016 7:06 AM
    Saturday, November 26, 2016 1:43 PM
  • Oh, I see.

    The -1 represent, for example, denotes one position to the left.

    A generic example would be like this:

       If lngPos > 0 Then
            strText = Left(strText, lngPos - n)
        End If

    where n is any number of characters to the left, and + n means any number of characters to the right. Correct?

    Regards,

    JohnDBCTX


    jp


    • Edited by JohnDBCTX Friday, November 25, 2016 11:20 PM
    • Marked as answer by JohnDBCTX Saturday, November 26, 2016 10:10 AM
    Friday, November 25, 2016 11:19 PM

All replies

  • Your code makes no sense. It initializes strX(1) to "asdfasdfXasdfasdf" and then, before testing anything, changes the selection in the document to this string.

    Perhaps you meant this:

    Sub modTwo()
        Dim strText As String
        Dim lngPos As Long
        strText = "asdfasdfXasdfasdf"
        lngPos = InStr(1, strText, "X", vbTextCompare)
        If lngPos > 0 Then
            strText = Left(strText, lngPos - 1)
        End If
        Selection.TypeText strText
    End Sub

    Or, since the value "asdfasdfXasdfasdf" is fixed, you might as well use

    Sub modTwo()
        Selection.TypeText "asdfasdf"
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by JohnDBCTX Friday, November 25, 2016 11:06 PM
    Friday, November 25, 2016 10:51 PM
  • What does the -1 represent in the Left function?

    Can you provide me some detail if you can?

    Regards,

    JohnDBCTX


    jp

    Friday, November 25, 2016 11:08 PM
  • Oh, I see.

    The -1 represent, for example, denotes one position to the left.

    A generic example would be like this:

       If lngPos > 0 Then
            strText = Left(strText, lngPos - n)
        End If

    where n is any number of characters to the left, and + n means any number of characters to the right. Correct?

    Regards,

    JohnDBCTX


    jp


    • Edited by JohnDBCTX Friday, November 25, 2016 11:20 PM
    • Marked as answer by JohnDBCTX Saturday, November 26, 2016 10:10 AM
    Friday, November 25, 2016 11:19 PM
  • Yes, indeed.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by JohnDBCTX Wednesday, December 14, 2016 7:06 AM
    Saturday, November 26, 2016 1:43 PM