Home Dynamic Last Row
Reply: 0

Dynamic Last Row

user825
1#
user825 Published in May 20, 2018, 11:56 am

I'm using the following code to find the last row in Sheet1 & Sheet2

Dim 1LastRow As Long
Dim 2LastRow As Long
1LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
2LastRow = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row

This works as initially expected, however I'd like "1LastRow" to adjust to dynamically pick up the last row.

For example, if Sheet1 initially has 50 rows, but after:

Sheets("Sheet2").Range("B2:B" & 2LastRow).Copy
Sheets("Sheet1").Range("D2").PasteSpecial xlPasteValues 

Sheet1 now has 55 rows. However, "1LastRow" is still assuming that Sheet1 only has 50 rows, and thus creates inconsistencies throughout the rest of the code when "1LastRow" is used. Any input is greatly appreciated!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.344233 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO