Rather than go through all 65536 rows to do what you want, the use of End(xlUp) finds the last used row in a specific column. In this case, in Column D, it goes to the absolute last row [65536], then goes up from there until it finds data of some kind. Thus, the code works until it hits the last row of data. Since you have varying number of rows each time it is, run, this code will always work, no matter how many or how few rows you have.
Actually, in preparation for XL 2007, it would be better to not use 65536 hard code, and use Rows.Count, which will work even if there are 1,000,000+ rows (as in XL2007).