Excel 2010 VBA 给定列索引的列长度
鉴于其简单的描述,我发现很难找到答案。我希望这不是重复的,尽管可能是因为它很简单,而且我根本找不到答案。
在 VBA 中,我有数百列,我想知道它们的所有长度。我知道确实有“COLS”列。我想要这样的东西:
For i in COLS
length = 'Some one line formula to find the length of column i
'Some code that works with the value of length
Next i
长度是指非空单元格的数量...出于我的特定目的,列中不会有空白单元格,并且我希望计数的列中的所有单元格都将包含文本(所有其余的将是空的)。
对于这个看似简单的问题,我们将不胜感激!
编辑:我还想让它依赖于列索引(在上面的循环中将是“i”)。我并不总是知道专栏字母......
I am finding this surprisingly hard to find an answer for, given its simple description. I hope this is not a duplicate, though it probably is because of its simplicity, and I simply cannot find the answer.
In VBA, I have hundreds of columns, and I'd like to know all their lengths. I know there are exactly "COLS" columns. I want something like this:
For i in COLS
length = 'Some one line formula to find the length of column i
'Some code that works with the value of length
Next i
By length I mean the number of non-empty cells... For my specific purposes there will be no blank cells in the columns, and all the cells in the column I wish to count will contain text (all the rest will be empty).
Help would be much appreciated on this seemingly simple matter!
Edit: I also want to make this dependent on the column index (which will be 'i' in the loop above). I won't always know the column letter...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这将计算 VBA 中的
text个非空单元格:This will count
textnon-empty cells in VBA:最好使用在所有 Excel 版本中从最后一行自动运行的例程。
It is best to use a routine that works automatically from the last row in all Excel versions.
我认为您可以用来
查找 A 列中最后使用的行。
浏览每一列,您可以获得您想要的内容......
I think you could use
to find last used row in column A.
Navigating through every column you can have what you want...
您可以使用 Range(large row number, column).End(xlUP).row 找到最后一个非空行,其中 Large 行号应该足够大,以始终超出您上次使用的单元格。
You can find the last non-empty row using Range(large row number, column).End(xlUP).row where large row number should be large enough to always be beyond your last used cell.
您可以使用工作表函数 CountA 使用替代方法,如下所示
测试数据:
VBA 函数:
You can use an alternative approach using the worksheet function CountA as follows
Test Data:
VBA function: