'Insight to Solution for VBA Runtime Error 6

Newbie to Excel VBA here.

With the following code:

Dim I As Integer

I = 1

Do
    If (Cells(I, "A").Value = "Potatoes") Then

        MsgBox ("Potatoes! I found a Potato in row " & Str(I))
    End If
    I = I + 1

Loop While (Cells(I, "A").Value <> " ")

End Sub

After MsgBox gives the message "Potatoes! I found a Potato in row 3" "Potatoes! I found a Potato in row 17"

I keep getting runtime error 6, Overflow.

The same applies to the alternative code

Public Sub Do_Loop_Exercise1()

Dim I As Integer
Do
    If (Cells(I, "A").Value = "Potatoes") Then

        MsgBox ("Potatoes! I found a Potato in row " & Str(I))
    End If
    I = I + 1

Loop Until (Cells(I, "A").Value = " ")

End Sub

I tried changing data type of I to Long and instead got error '1004'; Application Defined or object defined error.

I have resolved to use the For/Next loop structure where ever possible in future but want to understand why the above 2 codes wont run without throwing up the runtime 6 error message or in the alternative (when changing I to Long), error 1004. Any insight will be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source