2015-01-07 3 views
0

я использовалопределить подсчитывать видимые строки для выполнения функций на видимые ячейках только

при г = 2 к activesheet.usedrange.rows.count

но в тот момент я фильтрация столбцов по определенным критериям, а затем положить некоторые Если условия я не могу определить одинаковым образом для видимых строк.

Просьба помочь

+1

возможно дубликат [Row рассчитывать на отфильтрованные данные] (http://stackoverflow.com/questions/17285897/row-count-on-the-filtered-data) – silentsurfer

ответ

0

Вот функция, я создал для подсчета видимых строк в отфильтрованный список, и он также работает с нефильтруемых диапазонов, где строки скрыты. Вы должны быть осторожны с работой с фильтрованными диапазонами. (Если строки являются смежными в исходных данных, тогда они могут быть смежными в отфильтрованных данных. Таким образом, три строки могут = 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 
Смежные вопросы