I have written a simple code to illustrate my predicament.
Sub test()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets(\"Sheet1\")
Dim k As Lon
k = sh.Range("A2", sh.Range("A1").End(xlDown)).Rows.Count
or
k = sh.Range("A2", sh.Range("A1").End(xlDown)).Cells.Count
or
k = sh.Range("A2", sh.Range("A1").End(xlDown)).Count
You should use UsedRange
instead like so:
Sub test()
Dim sh As Worksheet
Dim rn As Range
Set sh = ThisWorkbook.Sheets("Sheet1")
Dim k As Long
Set rn = sh.UsedRange
k = rn.Rows.Count + rn.Row - 1
End Sub
The + rn.Row - 1
part is because the UsedRange only starts at the first row and column used, so if you have something in row 3 to 10, but rows 1 and 2 is empty, rn.Rows.Count
would be 8
Probably a better solution is work upwards from the bottom:
k=sh.Range("A1048576").end(xlUp).row
CountRows = ThisWorkbook.Worksheets(1).Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count