CreamStat
CreamStat

Reputation: 2185

Strange output in simple function - Excel VBA

I have this simple function in Excel VBA.

Public Function ubi() As Integer
   Dim i As Integer
   For i = 7 To 10
       If IsNumeric(Cells(35, i).Value) Then
           ubi = i
       End If
   Next

   ubi = i

End Function

As you see, the values of i are supposed to be 7 or 8 or 9 or 10. But, when I test the function in the Excel Worksheet I find ubi()=11.

So, What could be the source of error in my code ?

Upvotes: 2

Views: 99

Answers (1)

Chrismas007
Chrismas007

Reputation: 6105

Remove the ubi = i from outside the For...Next loop The code keeps going while this is true: For i = 7 To 10 Every time you hit this Next it is incremental. When i gets increased to 11, the loop exits - but i is already set to 11.

Upvotes: 5

Related Questions