如何删除 VBA/Excel 中 X 行下方的所有内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11835997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I delete everything below row X in VBA/Excel?
提问by phan
I have a long variable X that contains a number. Say it's 415.
我有一个包含数字的长变量 X。说是415。
How do I delete everything on the worksheet from Row 415 and below?
如何从第 415 行及以下删除工作表上的所有内容?
I want to ensure my spreadsheet is clean in Row 415 and anything else that might be below it.
我想确保我的电子表格在第 415 行以及它下方的任何其他内容都是干净的。
How do I do this? Thanks.
我该怎么做呢?谢谢。
回答by danielpiestrak
It sounds like something like the below will suit your needs:
听起来像下面这样的东西将满足您的需求:
With Sheets("Sheet1")
.Rows( X & ":" & .Rows.Count).Delete
End With
Where X is a variable that = the row number ( 415 )
其中 X 是一个变量,它 = 行号( 415 )
回答by Zairja
Another option is Sheet1.Rows(x & ":" & Sheet1.Rows.Count).ClearContents
(or .Clear
). The reason you might want to use this method instead of .Delete
is because any cells with dependencies in the deleted range (e.g. formulas that refer to those cells, even if empty) will end up showing #REF
. This method will preserve formula references to the cleared cells.
另一种选择是Sheet1.Rows(x & ":" & Sheet1.Rows.Count).ClearContents
(或.Clear
)。您可能想要使用此方法而不是使用此方法的原因.Delete
是因为在已删除范围内具有依赖项的任何单元格(例如,引用这些单元格的公式,即使为空)最终将显示#REF
. 此方法将保留对已清除单元格的公式引用。
回答by Grikisjan
This function will clear the sheet data starting from specified row and column :
此函数将从指定的行和列开始清除工作表数据:
Sub ClearWKSData(wksCur As Worksheet, iFirstRow As Integer, iFirstCol As Integer)
Dim iUsedCols As Integer
Dim iUsedRows As Integer
iUsedRows = wksCur.UsedRange.Row + wksCur.UsedRange.Rows.Count - 1
iUsedCols = wksCur.UsedRange.Column + wksCur.UsedRange.Columns.Count - 1
If iUsedRows > iFirstRow And iUsedCols > iFirstCol Then
wksCur.Range(wksCur.Cells(iFirstRow, iFirstCol), wksCur.Cells(iUsedRows, iUsedCols)).Clear
End If
End Sub
回答by Philip Ostle
Any Reference to 'Row' should use 'long' not 'integer' else it will overflow if the spreadsheet has a lot of data.
对“行”的任何引用都应使用“长”而不是“整数”,否则如果电子表格有大量数据,它将溢出。