Excel 2010 VBA 给定列索引的列长度

发布于 2024-12-11 21:36:47 字数 442 浏览 0 评论 0原文

鉴于其简单的描述,我发现很难找到答案。我希望这不是重复的,尽管可能是因为它很简单,而且我根本找不到答案。

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

呆萌少年 2024-12-18 21:36:47

这将计算 VBA 中的 text 个非空单元格:

For i = 1 To Columns.Count
    n = WorksheetFunction.CountA(Columns(i))
Next

This will count text non-empty cells in VBA:

For i = 1 To Columns.Count
    n = WorksheetFunction.CountA(Columns(i))
Next
山色无中 2024-12-18 21:36:47

最好使用在所有 Excel 版本中从最后一行自动运行的例程。

Dim lngRow As Long
lngRow = Cells(Rows.Count, "A").End(xlUp).Row

It is best to use a routine that works automatically from the last row in all Excel versions.

Dim lngRow As Long
lngRow = Cells(Rows.Count, "A").End(xlUp).Row
油饼 2024-12-18 21:36:47

我认为您可以用来

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

查找 A 列中最后使用的行。
浏览每一列,您可以获得您想要的内容......

I think you could use

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

to find last used row in column A.
Navigating through every column you can have what you want...

寂寞花火° 2024-12-18 21:36:47

您可以使用 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.

鹿港巷口少年归 2024-12-18 21:36:47

您可以使用工作表函数 CountA 使用替代方法,如下所示

测试数据:

Col1 Col2 Col3
A    A    A
     B    B
C    C    C
D    D    D
E         E

VBA 函数:

Sub Test()

    Dim sht As Excel.Worksheet
    Set sht = Worksheets("Sheet1")

    Dim i As Integer
    Dim max As Integer
    Dim length As Integer
    max = 0
    For i = 1 To 3
        'CountA returns the number of non-blank cells in a range
        length = WorksheetFunction.CountA(sht.Columns(i))
        If length > max Then max = length
    Next i

MsgBox max - 1 ('remove 1 if you have column headers)

End Sub

You can use an alternative approach using the worksheet function CountA as follows

Test Data:

Col1 Col2 Col3
A    A    A
     B    B
C    C    C
D    D    D
E         E

VBA function:

Sub Test()

    Dim sht As Excel.Worksheet
    Set sht = Worksheets("Sheet1")

    Dim i As Integer
    Dim max As Integer
    Dim length As Integer
    max = 0
    For i = 1 To 3
        'CountA returns the number of non-blank cells in a range
        length = WorksheetFunction.CountA(sht.Columns(i))
        If length > max Then max = length
    Next i

MsgBox max - 1 ('remove 1 if you have column headers)

End Sub
や三分注定 2024-12-18 21:36:47
len = sheet1.Range("B" & Rows.Count).End(xlUp).Row ' gives the count of B column
len = sheet1.Range("A" & Rows.Count).End(xlUp).Row ' gives the count of A column
len = sheet1.Range("B" & Rows.Count).End(xlUp).Row ' gives the count of B column
len = sheet1.Range("A" & Rows.Count).End(xlUp).Row ' gives the count of A column
七七 2024-12-18 21:36:47
dim i as integer
For i = 1 To Columns.Count
    debug.print i, cells(i, rows.count).end(xlup).row
Next
dim i as integer
For i = 1 To Columns.Count
    debug.print i, cells(i, rows.count).end(xlup).row
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文