[英]SpecialCells(xlCellTypeVisible) not working in UDF
Based on the question posed by @Chips Ahoy, I decided to create a UDF to find the PercentRank of visible cells in a range. 基于@Chips Ahoy提出的问题 ,我决定创建一个UDF来查找范围内可见单元格的PercentRank。
While @Chips seems happy with my syntax correction, I am actually unable to get my UDF to work correctly. 虽然@Chips似乎对我的语法修正感到满意,但实际上我无法使我的UDF正常工作。
When I run the below, the two addresses output read identical. 当我运行以下时,两个地址输出读取相同。 In my example using a formula of
=VisiblePercentRank($A$2:$A$41,0.5)
, both addresses output to the immediate window read $A$2:$A$41
, despite rows 3 to 11 being hidden by an autofilter. 在我的示例中使用公式
=VisiblePercentRank($A$2:$A$41,0.5)
,两个地址输出到即时窗口读取$A$2:$A$41
,尽管自动过滤器隐藏了第3到第11行。
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 removing .Rows
: 还尝试删除
.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
Should the second output not read $A$2,$A$12:$A$41
or have I missed something? 如果第二个输出不是
$A$2,$A$12:$A$41
或者我错过了什么?
Using Excel/Office 2013, 64bit on Win7, 64bit. 使用Excel / Office 2013,64位Win7,64位。
BRAIN FRYING UPDATE 大脑煎炸更新
I have found that my UDF works if I run it from the immediate window: 我发现如果我从即时窗口运行它,我的UDF会起作用:
?VisiblePercentRank(range("A2:A41"),0.5)
$A$2:$A$41 $A$2:$A$11,$A$39:$A$41
0.207
But if run from an in-cell formula of =VisiblePercentRank(A2:A41,0.5)
: 但如果从单元格公式
=VisiblePercentRank(A2:A41,0.5)
:
$A$2:$A$41 $A$2:$A$41
It seems that SpecialCells
is known to fail in UDFs. 似乎已知
SpecialCells
在UDF中失败。 A few sources: 1 , 2 , 3 几个来源: 1 , 2 , 3
You'd have to create your own function. 你必须创建自己的功能。 Perhaps something like this:
也许是这样的:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.