【问题标题】:Get number of rows filled in particular column -VBA [duplicate]获取特定列中填充的行数-VBA [重复]
【发布时间】:2016-12-07 05:38:46
【问题描述】:

现在我可以使用 VBA 中的值获取填充的行数:

Rows(Rows.Count).End(xlUp).Row

但是,这一行只能让我知道第一列中的行数。除了任何给定的列,我该怎么做才能得到相同的结果?

谢谢。

编辑: 答案只是将列作为 Rows() 的参数传递

【问题讨论】:

  • Cells(Rows.Count, colNumOrLetterHere).End(xlUp).Row
  • 谢谢蒂姆!接受的答案

标签: arrays vba excel


【解决方案1】:

问题是如何获取特定列中填充的行数。这与获取列中最后使用的行号不同。

这里是如何计算一列中有多少个非空白单元格

WorksheetFunction.CountA(Columns(2))

有很多方法可以引用单元格范围。以下是一些示例:

Sub Examples()

    Debug.Print "The last used row in the second column"
    Debug.Print Columns(2).Rows(Rows.Count).End(xlUp).row
    Debug.Print
    Debug.Print "The last used row in the second column"
    Debug.Print Columns("B").Rows(Rows.Count).End(xlUp).row
    Debug.Print
    Debug.Print "The last used row in the second column using Find"
    Debug.Print Columns("B").Find(What:="*", SearchDirection:=xlPrevious).row
    Debug.Print
    Debug.Print "The last used row on the ActiveSheet using Find"
    Debug.Print Cells.Find(What:="*", SearchDirection:=xlPrevious).row
    Debug.Print
    Debug.Print "The last used row in the second column"
    Debug.Print Cells(Rows.Count, 2).End(xlUp).row
    Debug.Print
    Debug.Print "The last used row in the second column"
    Debug.Print Cells(Rows.Count, "B").End(xlUp).row
    Debug.Print
    Debug.Print "The last used row in the second column"
    Debug.Print Range("B" & Rows.Count).End(xlUp).row
    Debug.Print
    Debug.Print "The last used row on the ActiveSheet, this is not limited to Column A"
    Debug.Print Range("A1").SpecialCells(xlCellTypeLastCell).row

    Debug.Print
    Debug.Print "The number of non-blank cells in Column B"
    Debug.Print WorksheetFunction.CountA(Columns(2))

End Sub

【讨论】:

    【解决方案2】:
    Debug.Print Range("A1").SpecialCells(xlCellTypeLastCell).Row
    

    上面一行表示当前工作表中使用的行数。

    【讨论】:

      猜你喜欢
      • 2018-04-11
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 2013-01-17
      相关资源
      最近更新 更多