简体   繁体   English

SpecialCells(xlCellTypeVisible)在UDF中不起作用

[英]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 几个来源: 123

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.

相关问题 SpecialCells(xlCellTypeVisible) - SpecialCells(xlCellTypeVisible) 使用.Hidden或.SpecialCells(xlCellTypeVisible)来忽略隐藏的行 - 不工作 - Using .Hidden or .SpecialCells(xlCellTypeVisible) to Ignore Hidden Rows — Not Working 尽管使用了`SpecialCells(xlCellTypeVisible)`,但选择可见单元格不起作用 - Selecting Visible Cells is not working although using `SpecialCells(xlCellTypeVisible)` SpecialCells(xlCellTypeVisible)选择整个工作表 - SpecialCells(xlCellTypeVisible) choose the whole worksheet .SpecialCells(xlCellTypeVisible).Copy的更快替代方法 - A faster alternative to .SpecialCells(xlCellTypeVisible).Copy VBA:处理过滤的行和SpecialCells(xlCellTypeVisible)与将数据复制到新表中 - VBA: Working with filtered rows and SpecialCells(xlCellTypeVisible) vs copying data into new sheet 遍历过滤器中的枢轴项时,vba SpecialCells(xlCellTypeVisible)无法正常工作 - vba SpecialCells(xlCellTypeVisible) not working correctly when looping through pivot items in a filter 在 excel vba 中选择了 SpecialCells(xlCellTypeVisible) 额外行 - SpecialCells(xlCellTypeVisible) extra row is selected in excel vba SpecialCells(xlCellTypeVisible)还包括隐藏/过滤的单元格 - SpecialCells(xlCellTypeVisible) also includes hidden/filtered cells 无法获取xlcelltypevisible的范围类的Specialcells属性 - unable to get the Specialcells property of the range class for xlcelltypevisible
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM