简体   繁体   English

如何在Excel VBA中编写UDF以过滤一系列单元格?

[英]How can I write a UDF in Excel VBA to filter a range of cells?

I've gotten into the habit of marking outlying data by changing cell styles. 我已经习惯于通过更改单元格样式来标记外围数据。 I'd like to write a UDF in excel to take a Range of cells as input, and return the subset of that range that is not marked as an outlier. 我想在excel中编写UDF,以将一个单元格Range作为输入,并返回未标记为异常值的该范围的子集。

This is what I have tried: 这是我尝试过的:

Function ValidCells(rCells As Range) As Range
    Dim c As Range
    For Each c In rCells
        If c.Style <> "Bad" Then
            Set ValidCells = Range(c, ValidCells)
        End If
    Next
End Function

My intent is to be able to do =Sum(ValidCells(A1:D1)) , and have it only sum the non-styled data. 我的意图是能够执行=Sum(ValidCells(A1:D1)) ,并仅对未样式化的数据求和。

However, ValidCells seems to return an empty range every time. 但是, ValidCells似乎每次都返回一个空范围。 What am I doing wrong? 我究竟做错了什么?

Are you sure it's returning an empty range? 您确定它返回的是空范围吗? When I try running this, VBA raises an error on your 'Set' line. 当我尝试运行此命令时,VBA在“设置”行上引发错误。 If you're calling the routine as a UDF from the worksheet you won't see the VBA error, but the UDF should stop executing and return #VALUE!. 如果您从工作表中将例程作为UDF调用,则不会看到VBA错误,但是UDF应该停止执行并返回#VALUE!。

In any case, you can do what you want, but there is one big caveat. 无论如何,您都可以做自己想做的事,但是有一个很大的警告。 First, the code: 一,代码:

Function ValidCells(rCells As Range) As Range
    Dim valid As Range

    Dim c As Range
    For Each c In rCells
        If c.Style <> "Bad" Then
            If valid Is Nothing Then
                Set valid = c
            Else
                Set valid = Union(valid, c)
            End If
        End If
    Next

    Set ValidCells = valid
End Function

The idea is to build up a multi-area range using VBA's 'Union' method. 这个想法是使用VBA的“联盟”方法建立一个多区域范围。 So, for example, if I put a bad cell in C8, and call ValidCells(B7:D9), this returns the multi-area range $B$7:$D$7,$D$8,$B$8:$B$9,$C$9:$D$9. 因此,例如,如果我在C8中放置了一个错误的单元格,并调用ValidCells(B7:D9),则会返回多区域范围$ B $ 7:$ D $ 7,$ D $ 8,$ B $ 8:$ B $ 9, $ C $ 9:$ D $ 9。 You can then use the result with SUM just fine. 然后就可以将结果与SUM一起使用了。

The caveat is that changing cell styles won't trigger this UDF to recalculate. 需要注意的是,更改单元格样式不会触发此UDF进行重新计算。 Normally, you'd be able to add a line like this: 通常,您可以像这样添加一行:

    Call Application.Volatile(True)

to your UDF and it would recalc on every change to the workbook. 到您的UDF,它将对工作簿的每次更改重新计算。 However, it seems like changing a cell style doesn't qualify as a "change" for volatility purposes. 但是,出于波动性的目的,更改单元格样式似乎不符合“更改”的条件。 So, you can get what you want out of the UDF, but there appears to be no real way to make it work like a "normal" one as far as recalculation goes, even if you mark it as volatile. 因此,您可以从UDF中获得所需的内容,但是就重新计算而言,即使您将其标记为volatile,似乎也没有真正的方法可以使它像“正常”的那样工作。 You'll have to remain aware of that if you use it. 如果要使用它,则必须保持意识到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM