获取列中填充单元格的数量(VBA)

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

Getting the number of filled cells in a column (VBA)

vbaexcel-vbaexcel

提问by poeschlorn

My first column is an index, with monotonously increasing numbers, which ends at a non predictable point. Now I want to find out, how many entries this column has. Is there a better way than iterating throug this and watch out for an empty cell?

我的第一列是一个索引,数字单调递增,以不可预测的点结束。现在我想知道这个列有多少个条目。有没有比遍历这个并注意空单元格更好的方法?

采纳答案by Alex K.

One way is to: (Assumes index column begins at A1)

一种方法是:(假设索引列从 A1 开始)

MsgBox Range("A1").End(xlDown).Row

Which is looking for the 1st unoccupied cell downwards from A1 and showing you its ordinal row number.

这是从 A1 向下寻找第一个未占用的单元格并显示它的序号行号。

You can select the next empty cell with:

您可以选择下一个空单元格:

Range("A1").End(xlDown).Offset(1, 0).Select

If you need the end of a dataset (including blanks), try: Range("A:A").SpecialCells(xlLastCell).Row

如果您需要数据集的结尾(包括空格),请尝试: Range("A:A").SpecialCells(xlLastCell).Row

回答by MrKowz

If you want to find the last populated cell in a particular column, the best method is:

如果要查找特定列中最后填充的单元格,最好的方法是:

Range("A" & Rows.Count).End(xlUp).Row

This code uses the very last cell in the entire column (65536 for Excel 2003, 1048576 in later versions), and then find the first populated cell above it. This has the ability to ignore "breaks" in your data and find the true last row.

此代码使用整列中的最后一个单元格(对于 Excel 2003 为 65536,在更高版本中为 1048576),然后找到其上方的第一个填充单元格。这可以忽略数据中的“中断”并找到真正的最后一行。

回答by almog.ori

You can also use

你也可以使用

Cells.CurrentRegion

to give you a range representing the bounds of your data on the current active sheet

为您提供一个范围,表示当前活动工作表上的数据范围

Msdn says on the topic

msdn 说这个话题

Returns a Range object that represents the current region. The current region is a range bounded by any combination of blank rows and blank columns. Read-only.

返回代表当前区域的 Range 对象。当前区域是由空白行和空白列的任意组合界定的范围。只读。

Then you can determine the column count via

然后你可以通过确定列数

Cells.CurrentRegion.Columns.Count

and the row count via

和行数通过

Cells.CurrentRegion.Rows.Count

回答by AMIB

You may also use:

您还可以使用:

UsedRange.Rows.Count

回答by deva ruban

To find the last filled column use the following :

要查找最后一个填充的列,请使用以下命令:

lastColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column