Jens Bergvall
Jens Bergvall

Reputation: 1627

How do i increment a variable in a loop for every 500th element in the loop?

Lets say i have a loop:

for foo = 0 to ubound(bar)

     if foo = 500 then
         foobar = foobar +1
     end if

next foo

In reality bar can range anywhere from 1 to 152023 elements.

So by checking if foo = 500 i only get foobar incremented once.

I feel this is extreme malpractice by checking every static 500 number like below:

for foo = 0 to ubound(bar)

     if foo = 500 then
         foobar = foobar +1
     elseif foo  = 1000 then
         foobar = foobar +1
     elseif foo = 1500 then
         foobar = foobar +1
         ' Et cetera.
     end if

next foo

The question is related to a function in our legacy systems which gets Out of string space- and Out of memory exceptions when passed the entire bar as an input parameter. So i thought i'd loop through the function with 500 elements each time instead of 12010230123 elements at once.

So basically.

How do i increment a variable in a loop for every 500th element in the loop?

Upvotes: 2

Views: 444

Answers (2)

Hrqls
Hrqls

Reputation: 2951

If I understand the question correctly you want to loop with steps of 500 ?

You can do that as follows :

For foo = 0 To UBound(bar) Step 500
  'do your thing
Next foo

Upvotes: 2

Xantier
Xantier

Reputation: 306

You can use modulo to determine if the value of foo is divisible by 500 and the increment the value of foobar.

if (foo mod 500) = 0 then ...

Upvotes: 3

Related Questions