I am trying to remove all extra blank rows and columns from an excel file using Interop Library.
I followed this question Fastest method to remove Empty rows and Columns From Excel Files using Interop and i find it helpful.
But i have excel files that contains a small set of data but a lot of empty rows and columns (from the last non empty row (or column) to the end of the worksheet)
I tried looping over Rows and Columns but the loop is taking hours.
I am trying to get the last non-empty row and column index so i can delete the whole empty range in one line
XlWks.Range("...").EntireRow.Delete(xlShiftUp)
Note: i am trying to get the last row containing data to remove all extra blanks (after this row , or column)
Any suggestions?
Find
can be used. SeeGetLastIndexOfNonEmptyCell
.CountA
is used to determine if the cells are empty and union the entire rows/columns to one rows/columns range.The two functions for getting empty ranges of rows/columns could be refactored to one function, something like this:
And then just call it:
Note: for more informations have a look e.g. on this SO question.
Edit
Try to simply clear the content of all the cells which are after the last used cell.