vba 选择最后一行并清除内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23127936/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:47:53  来源:igfitidea点击:

Select last row and clears content

excel-vbavbaexcel

提问by Nicholas Agius

I have the following sheet in which user input data.

我有以下用户输入数据的工作表。

https://dl.dropboxusercontent.com/u/83126653/user%20form%201.png

https://dl.dropboxusercontent.com/u/83126653/user%20form%201.png

Now I am using this code to delete the last row

现在我正在使用此代码删除最后一行

Sub DeleteLastRow()

    Sheets("User Form Engine data 1 Entry").UsedRange. _
    Range("A2:K2").End(xlDown).Select
    Range(Selection, Selection.End(xlToRight)).ClearContents  

End Sub

I would like to clear all the last row if even one cell from the row has something in it. need help. Thanks in advance

如果行中的一个单元格中有内容,我想清除所有最后一行。需要帮忙。提前致谢

采纳答案by brettdj

Try using Findto find the last row. It is more robust than the xlDownand xlUpapproaches (see Return a range from A1 to the true last used cell)

尝试使用Find查找最后一行。它比xlDownxlUp方法更健壮(请参阅返回从 A1 到真正上次使用的单元格的范围

Last row in columns A:K to be deleted

要删除 A:K 列中的最后一行

Sub KillLast()
Dim ws As Worksheet
Dim rng1 As Range
Set ws = Sheets("User Form Engine data 1 Entry")
Set rng1 = ws.Range("A:K").Find("*", ws.[a1], xlValues, , xlByRows, xlPrevious)
If Not rng1 Is Nothing Then rng1.EntireRow.Delete
End Sub

Last row in worksheet to be deleted

要删除的工作表中的最后一行

Sub KillLast()
Dim ws As Worksheet
Dim rng1 As Range
Set ws = Sheets("User Form Engine data 1 Entry")
Set rng1 = ws.UsedRange.Find("*", ws.[a1], xlValues, , xlByRows, xlPrevious)
If Not rng1 Is Nothing Then rng1.EntireRow.Delete
End Sub