For loop starts with "to" value

F-150 Phill

New Member
Joined
May 19, 2011
Messages
17
I've been searching for an answer to this for some time but the keywords always bring back the wrong results:???:.

I'm trying to run a simple for loop but when it starts running it starts with the "to" value:confused:.

i.e.
For A = Top to Bottom
do something
Next A

In this example, lets say "Top" = 5 and "Bottom" = 20, on the first run of the loop it will start with the 20 and count up from there (A=20, A=21, A=22 etc). "Top" and "Bottom" are variables with the values set earlier in the macro and the "Top" value is always smaller than the "Bottom" value.

If I write the loop like this instead:

For A = Bottom to Top
do something
Next A

where "Top" is still 5 and "Bottom" is still 20, it will start with the value for "Top" and count on (A=5, A=6 etc).

It doesn't work this way for all my loops (there are multiple in this macro), only this one, and this has happened before. What am I doing wrong? Am I using variables that are preprogrammed to do other things? I'm beating my head against the wall trying to figure it out!:banghead:
 
So I added some new code to give a picture of what I am working with. It will take a "snapshot" of all the values and put them in a message box.

Q = 0
For TWK = VSTop(VSGROUP) To VSBottom(VSGROUP)
For LWK = Top To BOTTOM
Q = Q + 1 'This will count the number of times through the loop.
MsgBox ("Q (Iteration of loop): " & Q & vbCr & "TWK: " & TWK & vbCr & "VSGROUP: " & VSGROUP & vbCr & "VSTop(VSGROUP): " & VSTop(VSGROUP) & vbCr & "VSBottom(VSGROUP): " & VSBottom(VSGROUP) & vbCr & "Top: " & Top & vbCr & "BOTTOM: " & BOTTOM & vbCr & vbCr & "LWK: " & LWK)
If Sheet1.Cells(TWK, 2).Value = Sheet1.Cells(LWK, 39).Value Then
Range(Sheet1.Cells(TWK, 27), Sheet1.Cells(TWK, 30)).Value = Range(Sheet1.Cells(LWK, 39), Sheet1.Cells(LWK, 42)).Value
Range(Sheet1.Cells(LWK, 39), Sheet1.Cells(LWK, 42)).ClearContents
Exit For
End If
Next LWK
Next TWK

Here's the first time through:
untitled-i2627.jpg


Here's the second time through:
untitled2-i2628.jpg


I'm sure you get the picture.
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Ok, I figured it out. As soon as I set:

Dim Top As Integer, Bottom As Integer

it worked correctly. It must have something to do with the variable "Top" having a preset use (I'm guessing). If you type "TOP" in with all caps withouth declaring it as a Dim type, it will change the case to a capital T and lower case o and p (type: "TOP" auto changes to "Top"). There's something at work here that is beyond my knowledge level, but it works now.

Thank you for your help.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,912
Members
452,949
Latest member
beartooth91

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