VBA(Excel):获取范围中的最后一个非空行

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

VBA (Excel): Get last non-empty row in a Range

excelvbaexcel-vbarowcells

提问by user1205577

I have an Excel spreadsheet that contains data similar to the following: enter image description here

我有一个 Excel 电子表格,其中包含类似于以下内容的数据: 在此处输入图片说明

I want to do a certain set of operations for each category for each of the names. I am guaranteed to have an empty cell before each new set of categories (but not necessarily the names), so when I iterate over the names, I do the following to find out what the last category row is:

我想为每个名称的每个类别执行一组特定的操作。我保证在每组新类别之前都有一个空单元格(但不一定是名称),因此当我遍历名称时,我执行以下操作以找出最后一个类别行是什么:

rowNum = sheetVariable.Cells(theName.Row + 1, 2).End(xlDown).Row

This works great except that if there is only onecategory for a name (such as for name4in the example image), it hops down to the next category of the next name.

这很有效,除了如果名称只有一个类别(例如name4示例图像中的for ),它会跳到下一个名称的下一个类别。

Is there a way to do this so that it also works for the case where there is only one category row for a person?

有没有办法做到这一点,以便它也适用于一个人只有一个类别行的情况?

回答by Tim Williams

Check to see if

检查是否

sheetVariable.Cells(theName.Row + 2, 2) 

is empty: if not then you can use the .End(xlDown)approach, otherwise the last row is theName.Row + 1

为空:如果不是,则可以使用该.End(xlDown)方法,否则最后一行是theName.Row + 1