Forum Discussion

ramkishore's avatar
ramkishore
Frequent Visitor
9 years ago

how can I come out of loop once my condition was satisfied

Dim columnCount,rowCount
  'Get ColumnCount and Row Count
  columnCount = GridObj.wColumnCount
  rowCount = GridObj.wRowCount
  
  For i = 0 to rowCount
    Log.Message(GridObj.wValue(i, ColumnName))
    Log.Message(ColumnValue)
     If GridObj.wValue(i, ColumnName) = ColumnValue Then
        GridObj.ClickCell i,ColumnName
        Exit for
      else  
       log.Error("expectedValues Does not exists")
      end if
  Next
    

I have to come out of the loop once my condition was satisfied. if suppose my condtion was satisfying at 12 column I mean 12 iteration. am getting 11 times error message here am facing some difficulty, if its not satisfied till the end of loop then only its should give error message.. please give me solution

 

2 Replies

  • Your code is correct, but to log an error only once you'll need to check the "if found" condition outside the loop, for example:

    Dim row
    row = -1 For i = 0 to GridObj.wRowCount If GridObj.wValue(i, ColumnName) = ColumnValue Then row = i Exit For End If Next If row >= 0 Then GridObj.ClickCell i, ColumnName Else Log.Error "Not found" End If

    Or better yet, use FindRow instead of searching manually:

    Dim row
    row = GridObj.FindRow(ColumnName, ColumnValue)
    If row >= 0 Then
      GridObj.ClickCell row, ColumnName
    Else
      Log.Error "Not found"
    End If
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Try this...

     

    https://www.tutorialspoint.com/vbscript/vbscript_exit_for_statement.htm

     

    I mean, you have it started... you just need to put the condition of your "IF" statement as the condition for your Exit For...

     

    As for the error condition of if it's not found, that I think you need to handle outside of your loop... to do some sort of work to indicate that the loop didn't find what it was looking for. A trick is to create a variable before the loop, set it to false, then, when you find it, set it to true.  So, it would be a combination of your if statement to set the variable and then adding the necessary Exit For