WR D
WR D

Reputation: 39

While loop ignoring parameters

I am trying to create a loop that stops a number from incrementing whilst it is lower than another number, a simple y<=x, but for some reason it just keeps going.

iv = 0
While iv <= vIC
     oExcel.Cells(1,1).value= vURL & iv
     oExcel.Cells(iv,2).value= vFileName & iv
     oExcel.Cells(iv,3).value= vIC
     iv = iv + 1
Wend

Upvotes: 0

Views: 46

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200313

Most likely your input is a string, so comparing an integer to it always produces the result False. To avoid this you need to convert the string to an integer:

iv = 0
While iv <= CLng(vIC)
    oExcel.Cells(1,1).value= vURL & iv
    oExcel.Cells(iv,2).value= vFileName & iv
    oExcel.Cells(iv,3).value= vIC
    iv = iv + 1
Wend

or use a For loop which implicitly converts string arguments to integers:

For iv = 0 To vIC
    oExcel.Cells(1,1).value= vURL & iv
    oExcel.Cells(iv,2).value= vFileName & iv
    oExcel.Cells(iv,3).value= vIC
Next

Upvotes: 1

Related Questions