SpecialCells (xlCellTypeVisible) does not work in UDF

Based on a question posed by @Chips Ahoy, I decided to create a UDF to find the PercentRank of the visible cells in the range.

While @Chips seems pleased with my syntax correction, I actually can't get my UDF to work correctly.

When I run the following, the two output addresses are considered identical. In my example, using the formula =VisiblePercentRank($A$2:$A$41,0.5), both addresses displayed in the direct window are read $A$2:$A$41, despite the fact that lines 3 through 11 are hidden by the autofilter.

Code:

Function VisiblePercentRank(x As Range, RankVal As Double)
    Debug.Print x.Address, x.Rows.SpecialCells(xlCellTypeVisible).Address
    VisiblePercentRank = WorksheetFunction.PercentRank(x.Rows.SpecialCells(xlCellTypeVisible), RankVal)
End Function

Also tried to remove .Rows:

Function VisiblePercentRank(x As Range, RankVal As Double)
    Debug.Print x.Address, x.SpecialCells(xlCellTypeVisible).Address
    VisiblePercentRank = WorksheetFunction.PercentRank(x.SpecialCells(xlCellTypeVisible), RankVal)
End Function

If the second conclusion is not readable $A$2,$A$12:$A$41or am I missing something?

Using Excel / Office 2013, 64 bit on Win7, 64 bit.

BRAIN TREATMENT UPDATE

, UDF , :

?VisiblePercentRank(range("A2:A41"),0.5)
$A$2:$A$41    $A$2:$A$11,$A$39:$A$41
 0.207 

=VisiblePercentRank(A2:A41,0.5):

$A$2:$A$41    $A$2:$A$41
+6
1

, SpecialCells, , UDF. : 1, 2, 3

. , - :

Function VisiblePercentRank(x As Range, RankVal As Double)
    Debug.Print x.Address, VisibleCells(x).Address
    VisiblePercentRank = WorksheetFunction.PercentRank(VisibleCells(x), RankVal)
End Function

Private Function VisibleCells(rng As Range) As Range
    Dim r As Range
    For Each r In rng
        If r.EntireRow.Hidden = False Then
            If VisibleCells Is Nothing Then
                Set VisibleCells = r
            Else
                Set VisibleCells = Union(VisibleCells, r)
            End If
        End If
    Next r
End Function
+4

Source: https://habr.com/ru/post/1016333/


All Articles