我有一个工作簿,我经常编辑和清除。有一次我可能把100 000
数据行放入其中,另一次 - 只有200
行。清除我使用的工作簿
.Range("A2:AO100000").Clear
它删除了单元格的可见内容,但仍然存在一些东西 - 使用键Ctrl+End
导致跳转到远高于当前可见范围的单元格。
我需要一种方法来确保删除之前编辑的所有数据,以便Ctrl+End
证明它。
我会使用ActiveSheet.UsedRange.Offset(1, 0).Clear
去除数据(只要UsedRange没有扩展到最后一个可用的行)。使用Offset(1, 0)
意味着保留标题行。
然后使用ActiveSheet.UsedRange
重置最后一个单元格。
从您的示例代码中,您似乎只想保留标题行。如果是这种情况,请尝试:
Sub KeepHeaderRow()
Rows("2:" & Rows.Count).Delete
End Sub
Sub DeleteUnusedFormats()
Dim lLastRow As Long, lLastColumn As Long
Dim lRealLastRow As Long, lRealLastColumn As Long
'Delete from used range rows & columns that have no data
'Detect end of used range including empty formatted cells
With Range("A1").SpecialCells(xlCellTypeLastCell)
lLastRow = .Row
lLastColumn = .Column
End With
'Find end of cells with data
lRealLastRow = _
Cells.Find("*", Range("A1"), xlFormulas, , xlByRows, xlPrevious).Row
lRealLastColumn = _
Cells.Find("*", Range("A1"), xlFormulas, , xlByColumns, xlPrevious).Column
'If used range exceeds data, delete unused rows & columns
If lRealLastRow < lLastRow Then
Range(Cells(lRealLastRow + 1, 1), Cells(lLastRow, 1)).EntireRow.Delete
End If
If lRealLastColumn < lLastColumn Then
Range(Cells(1, lRealLastColumn + 1), _
Cells(1, lLastColumn)).EntireColumn.Delete
End If
ActiveSheet.UsedRange 'Resets LastCell
End Sub
Excel在清除数据后保留使用的范围,因此ctrl + end
进入excel实例中使用的最后一个范围的原因。所有数据都已清除,没有任何特殊的单元格被保留以证明它在清除数据后显示Page Break Preview
并且它只显示标题。要永久删除保留的已使用范围,您必须保存工作簿,关闭它,然后在清除数据后重新打开它。您会注意到滚动条再次变大,因为保留的范围已被重置。