locked
Infinite loop! RRS feed

  • Question

  • Hello guys!

    So I'm making a code to extract datas off of an Excel file and I'm using Visual Studio and an API method.
    My excel file is built like that : 1st column = name of the object, 2nd column = data 2...
    So I made a test with InStr to check the 1st column but it seems I made an infinite loop, but I can't see why.

    Here is my code:

            Dim xlApp As New Excel.Application
            Dim xlWorkBook As Excel.Workbook
            Dim xlWorkSheet As Excel.Worksheet
            Dim range As Excel.Range
            Dim ligne As Excel.Range
            Dim i As Integer
    
            xlWorkBook = xlApp.Workbooks.Open(TextBox1.Text)
            xlWorkSheet = xlWorkBook.Worksheets("Run Properties")
    
            range = xlWorkSheet.UsedRange
            ligne = range.Rows
            For i = 1 To ligne.Count
                If InStr(1, range.Cells(i, 1).Value2, "coude", CompareMethod.Text) <> 0 Then
                    MsgBox("l'objet est un coude")
                Else
                    MsgBox("l'objet n'est pas un coude")
                End If
    
            Next i

    So the file is searched and it shows the MessageBox but on an infinite way.

    How can I fix it?

    Best regards,
    Nicolas


    Wednesday, July 6, 2016 7:40 AM

Answers

  • Make sure that the loop does not contain other parts that are not shown here.
    Wednesday, July 6, 2016 8:24 AM
  • How can I verify that?
    Include i in the message box message. Eg,
      MsgBox("l'objet est un coude " & i.ToString)
    • Edited by Acamar Wednesday, July 6, 2016 8:41 AM sp
    • Marked as answer by Nicolas Michelet Wednesday, July 6, 2016 8:47 AM
    Wednesday, July 6, 2016 8:40 AM

All replies

  • Check (display) the value of ligne.Count. It should not be a very large number.


    • Edited by Viorel_MVP Wednesday, July 6, 2016 7:55 AM
    Wednesday, July 6, 2016 7:54 AM
  • Ok, I just put a MsgBox(ligne.count) and it shows 116.

    But inside the loop, it goes way beyond 116 msgboxes showed...

    Wednesday, July 6, 2016 7:59 AM
  • Make sure that the loop does not contain other parts that are not shown here.
    Wednesday, July 6, 2016 8:24 AM
  • How can I verify that?
    Wednesday, July 6, 2016 8:35 AM
  • How can I verify that?
    Include i in the message box message. Eg,
      MsgBox("l'objet est un coude " & i.ToString)
    • Edited by Acamar Wednesday, July 6, 2016 8:41 AM sp
    • Marked as answer by Nicolas Michelet Wednesday, July 6, 2016 8:47 AM
    Wednesday, July 6, 2016 8:40 AM
  • Ok, actually I'm in fault this case...

    I did as you said and the loop is not infinite, I was clicking on enter too much, so each time it was going back on my form and since I was still clicking on enter on my keyboard, it loaded the loop again.

    Thanks for the help though!

    Tl:dr : I cliked too much on enter!

    Wednesday, July 6, 2016 8:46 AM