Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何正确计算VBA Excel中的可见行数?_Excel_Vba - Fatal编程技术网

如何正确计算VBA Excel中的可见行数?

如何正确计算VBA Excel中的可见行数?,excel,vba,Excel,Vba,请考虑excel中的以下列表: 其目的是统计那些可见的行。所以我有这个模块(宏): 正如您在消息框中看到的,它不包括所有可见行。我的代码中缺少了什么?希望有人能帮忙。假设您的当前区域是A3:B13,ws.Range(“A3”).CurrentRegion.Rows.Count将返回11(因为范围包含11行)。现在,您将从工作表的第4行循环到第11行,而不是范围,因此您将只看到第11行之前的数据,而不是第13行 以下代码使用了这样一个事实,即每个范围都有一个Cells-属性,并在该范围的单元格上

请考虑excel中的以下列表:

其目的是统计那些可见的行。所以我有这个模块(宏):


正如您在
消息框中看到的,它不包括所有可见行。我的代码中缺少了什么?希望有人能帮忙。

假设您的当前区域是
A3:B13
ws.Range(“A3”).CurrentRegion.Rows.Count
将返回11(因为范围包含11行)。现在,您将从工作表的第4行循环到第11行,而不是范围,因此您将只看到第11行之前的数据,而不是第13行

以下代码使用了这样一个事实,即每个范围都有一个
Cells
-属性,并在该范围的单元格上循环:

Dim r As Range, x As Long, rCount As Long, content As String
Set r = ws.Range("A4").CurrentRegion
For x = 2 To r.Rows.Count
    If Not r.Cells(x, 1).EntireRow.Hidden Then
        rCount = rCount + 1
        content = content & " " & r.Cells(x, 1)
    End If
Next
Debug.Print content
另一种方法是将
SpecialCells
-方法与参数
xlCellTypeVisible
-一起使用,该参数将仅返回某个范围内的可见单元格。但是,请注意,如果没有可见的单元格,这将导致运行时错误:

content = ""
Dim cell As Range
Set r = ws.Range("A10").CurrentRegion.Columns(1).SpecialCells(xlCellTypeVisible)
For x = 2 To r.Cells.Count
    content = content & " " & r.Cells(x)
Next
Debug.Print content

这回答了你的问题吗<代码>范围。特殊单元格(xlCellTypeVisible)
将是这里的正确方法。。。
content = ""
Dim cell As Range
Set r = ws.Range("A10").CurrentRegion.Columns(1).SpecialCells(xlCellTypeVisible)
For x = 2 To r.Cells.Count
    content = content & " " & r.Cells(x)
Next
Debug.Print content