Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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
Excel 定义可见行数以仅在可见单元格上实现函数_Excel_Vba - Fatal编程技术网

Excel 定义可见行数以仅在可见单元格上实现函数

Excel 定义可见行数以仅在可见单元格上实现函数,excel,vba,Excel,Vba,我曾经 对于activesheet.usedrange.rows.count的i=2 但当我根据某些条件筛选列,然后放入一些If条件时,我无法为可见行定义相同的方法 请帮助这里是我创建的一个函数,用于计算筛选列表中的可见行数,它还适用于隐藏行的非筛选范围。您必须小心使用过滤范围。(如果行在源数据中是连续的,那么它们在过滤数据中可能是连续的。因此,三行可以=1个区域。)为了绕过这一点,我在每个区域中循环,然后计算每个区域中的行 Function CountVisibleRowsInFiltered

我曾经

对于activesheet.usedrange.rows.count的i=2

但当我根据某些条件筛选列,然后放入一些If条件时,我无法为可见行定义相同的方法


请帮助

这里是我创建的一个函数,用于计算筛选列表中的可见行数,它还适用于隐藏行的非筛选范围。您必须小心使用过滤范围。(如果行在源数据中是连续的,那么它们在过滤数据中可能是连续的。因此,三行可以=1个区域。)为了绕过这一点,我在每个区域中循环,然后计算每个区域中的行

Function CountVisibleRowsInFilteredAreas(Optional myRange As Range)
Dim lCount As Long
Dim lCount2 As Long
Dim CurrRng As Range
Dim vArrRows As Variant
Dim vArrUnqRows As Variant
Dim iRow As Long
Dim iUnq As Long
Dim nUnq As Long
Dim rw As Range
Dim isUnq As Boolean

'if range to use was not specified in the code, then use the ActiveSheets' UsedRange
If myRange Is Nothing Then Set myRange = ActiveSheet.UsedRange

'this is created for autofiltered ranges, or non-autofiltered ranges with hidden rows,
'to count the number of rows that are visible.
'it assumes there is a header row and will count that as a row

'count the number of rows in each area to get the upper bound for the array that
 'will contain the row numbers of all of the rows in each area.
 lCount = 0
    For Each CurrRng In myRange.SpecialCells(xlCellTypeVisible).Areas
        lCount = lCount + CurrRng.Rows.Count
    Next CurrRng

'dim the array and give it upper and lower bounds, Set up a second counter to identify
'which row the loop is on, then loop through each row in each area,
'and get the row number and store it in an array.
ReDim vArrRows(1 To lCount)
    lCount2 = 0
    For Each CurrRng In myRange.SpecialCells(xlCellTypeVisible).Areas
        For Each rw In CurrRng.Rows
            lCount2 = lCount2 + 1
            vArrRows(lCount2) = rw.Row
        Next rw
    Next CurrRng

'remove duplicates/count unique rows
ReDim vArrUnqRows(1 To lCount2)
nUnq = 0
For iRow = 1 To lCount2
    isUnq = True
    'first one in vArrRows is always unique, and added to vArrUnqRows, the rest are compared
    'against the known unique ones in vArrUnqRows, if they are unique then they are added
    'to vArrUnqRows
    For iUnq = 1 To nUnq
        If vArrRows(iRow) = vArrUnqRows(iUnq) Then
            isUnq = False
            Exit For
        End If
    Next iUnq
    'add unique row numbers to vArrUnqRows
    If isUnq = True Then
        nUnq = nUnq + 1
        vArrUnqRows(iUnq) = vArrRows(iRow)
    End If
Next iRow
ReDim Preserve vArrUnqRows(1 To nUnq)
'creates an array of the row numbers of visible rows, but that is not used here and only the
'count of the visible rows is returned (nUnq)
CountVisibleRowsInFilteredAreas = nUnq

End Function
可能重复的