XL2010 for loops not working / ignored

rjwebgraphix

Well-known Member
Joined
May 25, 2010
Messages
590
Somehow all my for loops are now broken and I don't know how it happened. It's like XL is just ignoring it. If anyone has any clue why this is happening, please let me know.

Example: The following code as two loops, a for loop and a do loop. In theory both loops do exactly the same thing. In cells A2-A16 is merely the letter A thru P respectively just to keep it simple for this test. You would expect it would give me 32 subsequent msgboxes giving me the value of each cell, but it completely ignores the For loop and runs the Do loop as it should giving me only 16 message boxes starting at cell A16 (Value P) ending at cell A2 (Value A)

I don't see anything with the code that would cause it to not work, so it has to be something with XL causing it. It's a company load laptop, so it's not like I can uninstall and reinstall, I'd have to send it out of state for them to reload it and I'd rather avoid that if possible. Thanks for any input on this.

Code:
Sub Testit2()

Dim i As Long
Dim r As Long

For i = 16 To 2
    MsgBox Cells(i, "A").Value & " For Loop"
Next i

r = 16
Do While r <> 1
    MsgBox Cells(r, "A").Value & " Do Loop"
    r = r - 1
Loop

End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I'm not an expert at For Loops but I'm pretty sure that you can't go from 16 to 2. The next value after 16 is 17 so technically it already passed 2 and therefore never enters the For Loop. I am an expert at Do Loops and you did get the Do Loop correct. I prefer Do Loops when using a counter. For Loops I only use when I'm extracting all the data of an array because it's easier with a For Loop. But for counters like the one you are doing, it's easier to use a Do Loop.
 
Upvote 0
That should be:
Rich (BB code):
For i = 16 To 2 Step -1
    MsgBox Cells(i, "A").Value & " For Loop"
Next i

BAH! It is with the loop.... That's me trying to be in a hurry to get things done. The faster I go the slower I am. Thanks.
 
Upvote 0
Just out of curiosity, what exactly are you doing that requires the loop to go backwards?
 
Upvote 0
Just out of curiosity, what exactly are you doing that requires the loop to go backwards?

Deleting rows of data that aren't applicable to the form the rest of the data will be auto-filling.

Code:
Sub delnonGD()
Dim i As Long

For i = 16 To 2 Step -1
    If Cells(i, "T") <> "CustomerSpeciificNumberCriteria" Then
        Rows(i).EntireRow.Delete
    End If
Next i

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,640
Members
448,974
Latest member
DumbFinanceBro

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