I'm writing a method in VBA in Excel 2013 to loop through the rows in two worksheets and compare the text in a column from each. When I run my code, I find that the code loops through the entire worksheet, not just the rows with data.
Excel.Sheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell)
returns a cell in the correct column, but the row is the last row in the sheet (1048576), rather than the last row with data (1951).
I had written a check for empty cells (since I can't be sure that every row in the valid range is used), so it doesn't cause any errors, but as this method is called from inside Worksheet_Change
, it really slows things down.
Normally when the "last" cell is reported incorrectly a save usually fixes it, and if that doesn't, then deleting the rows (not just the contents, but the entire rows) from the mis-reported last cell back to the actual last cell and then saving works. However, in this instance, it is not helping.
I searched with Google without success. I'd like to not have to copy all the data and code out of this workbook and into a new one. Any ideas?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…