Using While...Wend structure RRS feed

  • Question

  • Now I may have known that if I do not use the While...Wend structure properly in file handling, it may result in an infinite loop.

    Can anyone provide me some examples on how to utilize it properly?


    Saturday, June 25, 2016 8:39 AM

All replies

  • Sub OutputSynonymsAndAntonyms()
    Dim strZ As String

    Open "C:\Users\John\Documents\Pages 13 to 59.csv" For Input As #1
    Open "C:\Users\John\Documents\Noun Synonyms.csv" For Output As #13
    Open "C:\Users\John\Documents\Noun Antonyms.csv" For Output As #14
    While Not EOF(1)
        Input #1, strZ
            If InStr(1, strZ, "-Ant.", vbBinaryCompare) Then
            End If
    Close #14, 13, 1

    End Sub

    So far, I keep getting errors on how to utilize a Do...Loop or While...Wend.  I just don't know which would be more suitable.  What I want my program is the following:

    while not EOF(1)


              If instr(1, strz, "Ant.", vbBinaryCompare) then

                        do until instr(1, strz, ";", vbBinaryCompare)

                                  if strz=";" then

                                     exit do

                                  end if

                end if


    However, it has given me infinite loops.  How could fix this code snippet?


    Saturday, June 25, 2016 9:55 AM
  • What do you want to do? Read data from file and write in another 2 files only lines that meet a criteria? -

    Saturday, June 25, 2016 11:18 AM
  • If you want to use Exit Do then you need to do

    Do While Not EOF(1)

         Exit Do


    If you don't need Exit Do then you can use either While or Do While.

    Saturday, June 25, 2016 11:21 AM