vba code explanation

jtsains

Board Regular
Joined
Apr 29, 2011
Messages
103
Does anyone know the difference between these two codes?
Code:
For i = 2 To Range("D" & Rows.Count).End(xlUp).Row
and
Code:
For i = 2 To Range("A2").End(xlDown).Row
can you please help me understand what is happening with each?Thank you,Jeff
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Code 2 has a cell in column A as the reference cell instead of D... :)

But on a more interesting note, code 1 will go to the last row, which is based on contents in column D. You determine the last row by going bottom up from the very last row of the sheet until you find a non-empty cell.

Code 2 is similar but you start in cell A2. You go downwards until to encounter an empty cell in column A. Which is not necessarily the same as in code 1 (even if you used the same column A or D).
 
Upvote 0
Thank you Wigi,I understand the A vs D column. Does the ".Count" indicate that it starts at the bottom and goes up?I have a string of data in one column and a header. I want to run the code with the For/Next statement until the end of the data in that column but it must start with Row 2, and not include A1 or D1 due to the headers. Will either code create problems with what I am trying to do?Thank you again for your help.JEff
 
Upvote 0
Hi Jeff

If you have no empty cells in the column, between row 2 and the last row of data, both methods will be similar. You can choose either method.
 
Upvote 0

Forum statistics

Threads
1,203,751
Messages
6,057,147
Members
444,908
Latest member
Jayrey

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