VBA 转到最后一个空行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19238596/
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
VBA Go to last empty row
提问by r.paul ?
I have a project on excel macro, I need to highlight the next last row that has an empty value. example cell A1:A100 have data and the next cell is A101 is empty.
我有一个关于 excel 宏的项目,我需要突出显示具有空值的最后一行。示例单元格 A1:A100 有数据,下一个单元格 A101 为空。
when user click a button it should highlight the cell A101...
当用户单击按钮时,它应该突出显示单元格 A101 ...
回答by AdmiralWen
If you are certain that you only need column A, then you can use an End function in VBA to get that result.
如果您确定只需要 A 列,那么您可以使用 VBA 中的 End 函数来获得该结果。
If all the cells A1:A100 are filled, then to select the next empty cell use:
如果所有单元格 A1:A100 都已填充,则要选择下一个空单元格,请使用:
Range("A1").End(xlDown).Offset(1, 0).Select
Here, End(xlDown) is the equivalent of selecting A1 and pressing Ctrl + Down Arrow.
在这里,End(xlDown) 相当于选择 A1 并按 Ctrl + 向下箭头。
If there are blank cells in A1:A100, then you need to start at the bottom and work your way up. You can do this by combining the use of Rows.Count and End(xlUp), like so:
如果 A1:A100 中有空白单元格,那么您需要从底部开始向上工作。您可以通过结合使用 Rows.Count 和 End(xlUp) 来做到这一点,如下所示:
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
Going on even further, this can be generalized to selecting a range of cells, starting at a point of your choice (not just in column A). In the following code, assume you have values in cells C10:C100, with blank cells interspersed in between. You wish to select all the cells C10:C100, not knowing that the column ends at row 100, starting by manually selecting C10.
更进一步,这可以推广到选择一系列单元格,从您选择的点开始(不仅仅是在 A 列中)。在以下代码中,假设单元格 C10:C100 中有值,中间散布着空白单元格。您希望选择所有单元格 C10:C100,但不知道该列在第 100 行结束,首先手动选择 C10。
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlUp)).Select
The above line is perhaps one of the more important lines to know as a VBA programmer, as it allows you to dynamically select ranges based on very few criteria, and not be bothered with blank cells in the middle.
上面这行可能是作为 VBA 程序员需要了解的更重要的行之一,因为它允许您根据很少的条件动态选择范围,而不会被中间的空白单元格所困扰。
回答by L42
try this:
尝试这个:
Sub test()
With Application.WorksheetFunction
Cells(.CountA(Columns("A:A")) + 1, 1).Select
End With
End Sub
Hope this works for you.
希望这对你有用。
回答by Maycow Moura
This does it:
这样做:
Do
c = c + 1
Loop While Cells(c, "A").Value <> ""
'prints the last empty row
Debug.Print c