Long Loop Accidentally Repeating Numbers

AlexSlivi

New Member
Joined
Jun 17, 2011
Messages
19
I was spot-checking the code I wrote, which goes through 17000 people, performs some calculations for them, and then spits the data out on another tab. I assigned an ID number to each person for easy checking.

Occasionally, in the loop, I'll spot things like (for ID):
456
457
457
459

Then I'll re-run it and that problem will be gone, but will appear somewhere else (or if I plug in 458, all the information I need is there and correct). Is this a CPU overflow issue? Any idea on how to correct this?

Thanks,
Alex
 
Kinda hard to guess without seeing the code ...
 
Upvote 0
I don't know how much the code will help, as most of the caluclations are done in the workbook, but here you are:

Code:
For x = 1 To 17402
Sheets("InputsAndResults").Range("B5").Value = x
Sheets("2011").Range("A3:F3").Offset(x, 0).Value = Sheets("Individual Results").Range("A3:F3").Value
Sheets("2011").Range("I3:AF3").Offset(x, 0).Value = Sheets("Individual Results").Range("G3:AD3").Value
Sheets("2011").Range("G3:H3").Offset(x, 0).Value = Sheets("Individual Results").Range("C10:D10").Value
Next
 
Upvote 0
Presumably changing InputsAndResults!B5 causes a calculation to occur on Individual Results?

Code:
    With Worksheets("2011")
        For x = 1 To 17402
            Worksheets("InputsAndResults").Range("B5").Value = x
            Worksheets("Individual Results").Calculate
            .Range("A3:F3").Offset(x).Value = Worksheets("Individual Results").Range("A3:F3").Value
            .Range("G3:H3").Offset(x).Value = Worksheets("Individual Results").Range("C10:D10").Value
            .Range("I3:AF3").Offset(x).Value = Worksheets("Individual Results").Range("G3:AD3").Value
        Next x
    End With

Shouldn't be necessary, though.
 
Upvote 0
I don't think that's going to work, because I also have the loop doing the same things for the 2012 and 2013 sheets (I just took them out to prevent a huge wall of text--I'm sorry about that). Maybe something like this would work (to repeat the step), but I don't think I can change x from inside the loop can I?

Code:
If Worksheets("Individual Results").Range("B3").Value <> x Then
x = x - 1
End If
 
Upvote 0
You're welcome, but I don't have enough information to make a suggestion -- except to say it is never correct to change the loop index variable inside a loop.
 
Upvote 0
Okay. For anyone who stumbles upon this later, I restarted my computer and it worked fine (I've coded it to notify me if it the loop makes this error, and I haven't been notified yet). So I'm guessing it was more of a processing error.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top