PSS ID Number: 134617
Article Last Modified on 8/19/2003
' Macro code starts here.
Sub Reset_LastCell()
' Save the lastcell and start there.
Set lastcell = Cells.SpecialCells(xlLastCell)
' Set the rowstep and column steps so that it can move toward
' cell A1.
rowstep = -1
colstep = -1
' Loop while it can still move.
While (rowstep + colstep <> 0) And (lastcell.Address <> "$A$1")
' Test to see if the current column has any data in any
' cells.
If Application _
.CountA(Range(Cells(1, lastcell.Column), lastcell)) _
> 0 Then colstep = 0 'If data then stop the stepping
' Test to see if the current row has any data in any cells.
' If data exists, stop row stepping.
If Application _
.CountA(Range(Cells(lastcell.Row, 1), lastcell)) _
> 0 Then rowstep = 0
' Move the lastcell pointer to a new location.
Set lastcell = lastcell.Offset(rowstep, colstep)
' Update the status bar with the new "actual" last cell
' location.
Application.StatusBar = "Lastcell: " & lastcell.Address
Wend
' Clear and delete the "unused" columns.
With Range(Cells(1, lastcell.Column + 1), "IV16384")
Application.StatusBar = "Deleting column range: " & _
.Address
.Clear
.Delete
End With
' Clear and delete the "unused" rows.
With Rows(lastcell.Row + 1 & ":16384")
Application.StatusBar = "Deleting Row Range: " & _
.Address
.Clear
.Delete
End With
' Select cell A1.
Range("a1").Select
' Reset the status bar to the Microsoft Excel default.
Application.StatusBar = False
End Sub
100406 Excel: Resetting Last Cell Fixes Memory and Printing Problems
Additional query words: 5.00a 5.00c large size convert conversion XL5 XL7 kbtshoot XL
Keywords: kbcode kbinfo kbProgramming KB134617
Technology: kbExcel500aMac kbExcel500c kbExcel95a kbExcel95Search kbExcelMacsearch kbExcelSearch kbExcelWinSearch kbHWMAC kbOSMAC kbVBASearch kbZNotKeyword3 kbZNotKeyword6