xlUp not working in For loop

Chr15

New Member
Joined
Jul 12, 2012
Messages
4
I have been tasked with debugging some code at work and have found an easy fix for it, but don't understand why it works..

The original code:
For x = 6 to Cells(10000, 1).End(xlUp).Row

The "repaired" code:
last_row = Cells(10000, 1).End(xlUp).Row
For x = 6 to last_row

It was originally not running the For loop, as if it made Cells(10000, 1).End(xlUp).Row = 0.
But now it works. I just want to know if there is a reason for this or if it's just a coincidence.:confused:
Thank you for your help.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi and Welcome to the Board,

I'm not spotting any difference between the expected result of those two versions.

If that's correct, it means you haven't really fixed the problem but instead are running the code under different conditions.

Possible problems with the code are..
1. If the last row is less than 6, the For statement will not execute any instructions.

2. Cells(10000,1) is not a fully qualified reference. If the Active Sheet is not the one you intended, the wrong row number will be returned.

This would be more reliable...
Code:
Sheets("Sheet1").Cells(Rows.Count,1).End(xlUp).Row

Good luck!
 
Last edited:
Upvote 0
Thanks.
The correct workbook and sheet are activated and there are over 2000 rows of data. It is the same data that wasn't working before.

The code you gave also works and I like it better, so I'm using it. These three bits of code all do the same thing so I don't see the difference either. Could it be that the
variable being assigned before the loop ensures that the calculation is complete before the loop is started?

I just thought that maybe there was some sort of Excel VBA issue with this sort of code that I didn't know about, you know a sort of preference that the language has. But if that's not the case then I don't know what it could be.
Thank you for your help
 
Upvote 0
These three bits of code all do the same thing so I don't see the difference either. Could it be that the variable being assigned before the loop ensures that the calculation is complete before the loop is started?

There are a few processes in Excel VBA that can be run multi-threaded, but the statements in your example will be executed sequentially. So good thinking in seeing that as a possible explanation, but that isn't the cause of the different results.

Well that will remain a mystery for now. I can't count all the times I'd swear the same code gave me different results only to later figure out what was different about the environment/ conditions that caused that. :)

If it happens again, try to save a copy of that workbook and note the conditions when it occurred (active sheet, other workbooks open, etc).
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,631
Members
449,241
Latest member
NoniJ

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