My Worksheet has 29 rows and 39 columns. Currently I use
lrow = ActiveSheet.UsedRange.Rows.count
--> for getting used rows count
lColumn = ActiveSheet.UsedRange.Columns.count
--> for getting used columns count
Excel gives wrong count every time it runs. Sometimes it gives:
Rows: 29 Columns: 784
On other runs it gives
Rows: 32755 and Columns as: 784
and on other runs it gives different values.
I have checked there is no junk data after 29 rows and after 39 columns. Also,
Previous to filling the data I clear the sheet with: ActiveWorkbook.Worksheets("Field Difference").Cells.Delete
I hope ActiveWorkbook.Worksheets("Field Difference").Cells.Delete
completely clears the sheet and clears the sheet of the junk data if any on the sheet. How else I can make sure that there is no junk data in the worksheet.
I do understand that we have other Options such as:
ActiveWorkbook.Worksheets("Field Difference").UsedRange.ClearContents
- to clear only contents
ActiveWorkbook.Worksheets("Field Difference").UsedRange.Clear
- to clear formatting as well.
Please do let me know why I am getting wrong values for the count of rows and columns and what is the way out. Can I use any other reliable way to get the UsedRange
row count and UsedRange
columns count.