简体   繁体   English

在列中查找下一个非空单元格

[英]Find next non-empty cell in column

So I have a column of data (player names) that I pasted from another Excel file.所以我有一列数据(玩家名称)是我从另一个 Excel 文件中粘贴过来的。 Cell A2 has the first player name and then there are several blank rows (the number of blank rows isn't the same for every roster sheet), and I am trying to determine the row number of the next non-empty cell with:单元格A2具有第一个玩家名称,然后有几个空白行(每个名册表的空白行数不同),我试图确定下一个非空单元格的行号:

nextPlayerRow = rosterSh.Columns("A").Find(What = "*", after:=rosterSh.Range("A2"), LookIn:=xlValues).row

where nextPlayerRow is declared as an Integer .其中nextPlayerRow被声明为一个Integer This, however, returns a Runtime Error 91.但是,这会返回运行时错误 91。

What am I doing wrong?我究竟做错了什么?

Run-time error 91 is Object variable not set.运行时错误 91 是未设置对象变量。 Possibly either you have not done a set to initialize rosterSh, or your .find is finding nothing, and therefore unable to return .row.可能是您没有完成初始化 rosterSh 的设置,或者您的 .find 没有找到任何内容,因此无法返回 .row。

When you use the .find method, (or any time you need a property of a method) you should first retrieve the object with a Set, and then check for the existence of the object, before trying to access one of its properties as in:当您使用 .find 方法时(或任何时候您需要方法的属性),您应该首先使用 Set 检索对象,然后在尝试访问其属性之一之前检查该对象是否存在,如:

Set nextPlayerCell = rosterSh.Columns("A").Find(What = "*", after:=rosterSh.Range("A2"), LookIn:=xlValues)
If Not nextPlayerCell is nothing then
    nextPlayerRow = nextPlayerCell.row
End If

您可以使用End(xlDown)而不是使用Find

nextPlayerRow = rosterSh.Range("A2").End(xlDown).Row

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM