SOLVED

vba cells

Iron Contributor

hello

i have a question, like i want to find last row number in a used range so when i use this code

Debug.Print ActiveSheet.UsedRange.Cells(rows.count, 5).End(xlUp).Row

 

it gives an error, although rows.count should give 1048576

 

but when i use 

Debug.Print ActiveSheet.UsedRange.Cells(1000000, 5).End(xlUp).Row it works fine and gives a number

 

why is that? please advise 

2 Replies
best response confirmed by Hans Vogelaar (MVP)
Solution
Range references are relative, so if your data is in A17:D30, then that is your usedrange. So, 1048576 rows down from row 17 is beyond excel's row limit and you'll get an error.

Try: ActiveSheet.Cells(rows.count, 5).End(xlUp).Row
great thanks a lot !
1 best response

Accepted Solutions
best response confirmed by Hans Vogelaar (MVP)
Solution
Range references are relative, so if your data is in A17:D30, then that is your usedrange. So, 1048576 rows down from row 17 is beyond excel's row limit and you'll get an error.

Try: ActiveSheet.Cells(rows.count, 5).End(xlUp).Row

View solution in original post