如何在Excel VBA中查找连续数据的单元格?
给定图像...如果我知道有一些数据从 Range("B3") 开始。
如何找到单元格 E3 之前具有连续数据的单元格?由于 F3 为空白,因此不应考虑 G3 及以后的内容。 结果可以是范围对象 (B3:E3) 或单元格计数(在本例中为 4)。
通过将 B3 设置为活动单元格并执行...
Range(ActiveCell, ActiveCell.End(xlToRight).Count
我确实得到了计数,但是此方法并不可靠,如果只有 B3 有数据,它会对单元格进行计数,直到工作表末尾。
当然,这也可以通过循环单元格来实现,但我宁愿使用工作表函数或其他一些有效的方法。
Given the Image... If I know that there is some data starting at Range("B3").
How can I find the cells with contiguous data that is till cell E3? Since F3 is blank G3 onwards should not be considered.
The result could either be a range object (B3:E3) or count of cells( 4 in this case).
By setting B3 as the Active cell and doing..
Range(ActiveCell, ActiveCell.End(xlToRight).Count
I do get the count, however this method is not reliable, in case only B3 has data it counts the cells till the end of the sheet.
Of course this could also be achieved by looping through the cells but I'd rather use a Worksheet Function or some other efficient method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您似乎正在尝试确定从单元格 B3 开始的一行中使用的连续列数。
下面的代码将根据您的数据返回 $B$3:$E$3 和 4 的值。如果只有单元格 B3 有数据,它将返回 $B$3 和 1。
It seems that you are trying to determine the number of continuous columns used by in a row, starting from cell B3.
The code below will return the values of $B$3:$E$3 and 4 based on your data. If only cell B3 has data, it will return $B$3 and 1.
<代码>
将返回 B3:E3。或者
rMyRange 还将返回 B3:E3
Will return B3:E3. Alternatively
rMyRange will also return B3:E3
您可以使用 CurrentRegion 属性。这将返回与指定范围相邻的范围。所以...
但是,如果您的数据位于第 4 行及以下(假设您的数据位于 B4:E6 中),那么您将得到这些结果
这是您想要的结果吗?
You could use the CurrentRegion property. This returns the range that is contiguous to the specified range. So...
However, if you had data in rows 4 and below (let's say you had data in B4:E6), then you would get these results
Is this what you were after?
我喜欢使用一个函数来计算包含值的列,直到遇到空单元格。返回值可用于设置 FOR NEXT 循环来搅动表。我将这样做:
I like to use a function that counts columns that contain values until it encounters an empty cell. The return value can be used to set up a FOR NEXT loop to churn through a table. Here is how I would do it:
根据您需要获得的通用程度,它可能很简单,
如果您想绑定 ActiveCell,请尝试
Depending on how general you need to get, it could be as simple as
If you want to tie in the ActiveCell, try