【问题标题】:Perform a find on hidden cells在隐藏的单元格上执行查找
【发布时间】:2011-06-09 18:40:36
【问题描述】:

我在一个隐藏列中有一个计算值范围,用于下拉框。为了弄清楚用户选择了哪个值,我尝试在该范围内运行 Find,但由于某种原因,只要该列被隐藏,Excel 就不会返回与其选择对应的单元格。

如何让 Find 在隐藏范围内的单元格上工作。请记住 - 我搜索的是单元格计算值,而不是公式。

以下不起作用:

Set inserted = Range("RDS_Event_IDs").Find(Range("SelectedEvent"), , xlValues, xlWhole)

只要Range("RDS_Event_IDs") 中的单元格被隐藏。

由于该解决方案必须在一般情况下工作,其中部分或全部正在搜索的范围可能会被隐藏,并且可能会搜索整个工作表,因此以编程方式取消隐藏所有受影响的行和列是不可行的然后重新隐藏之前隐藏的那些。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    根据Andy Pope(他永远不会错)如果您使用 xlFormulas,Find 仅适用于隐藏单元格。也许是匹配?

    Set inserted = Cells(Application.WorksheetFunction.Match("SelectedEvent", Range("RDS_Event_IDs"), 0), Range("RDS_Event_IDs").Column)
    

    【讨论】:

    • 稍微调整一下,我想去Set inserted = Range("RDS_Event_IDs").Cells(...Match...),但是是的,这绝对是一个很好的解决方法。感谢您的建议!
    • 这确实适用于隐藏单元格,但不适用于自动过滤的隐藏单元格。
    • 使用Application.Match 比使用WorksheetFunction.Match 更好,因为它可以正确处理错误。请参阅此答案:stackoverflow.com/a/17751568/224067Application.Match 在 VBA IDE 中没有为我自动完成,但它确实有效,并且具有常规 Match 函数的参数。
    【解决方案2】:

    功能方法

    使用 Doug Glancy 的回答,将其放入函数中以实现可重用性会很好。

    ''
    ' Find a range using `WorksheetFunction.Match()`. This alternative works well
    ' for finding range in hidden cells, and is not case sensitive.
    '
    ' Created this solution based on answer on Stack Overflow @see https://stackoverflow.com/a/6298404/8309643
    '
    ' @author Robert Todar <robert@roberttodar.com>
    ''
    Function Find(ByRef searchRange As Range, ByVal what As String) As Range
        Set Find = Cells(Application.WorksheetFunction.Match(what, searchRange, 0), searchRange.Column)
    End Function
    

    另一种搜索范围的方法是从范围中获取一个数组并循环它。同样,将它放在一个函数中可以很容易地重用!

    ''
    ' Finds a range based on it's value.
    ' This works faster than `Range.Find()` as it loops an array instead of cells.
    ' This also works for hidden cells where `Range.Find` does not.
    '
    ' Note, this looks for first match, and is case sensitive by defaut, unless
    ' Option Match Case is used at the top of the module it is stored in.
    '
    ' @author Robert Todar <robert@roberttodar.com>
    ''
    Public Function FindFast(searchRange As Range, what As String) As Range
        ' Get data from range into an Array. Looping Arrays is much
        ' faster than looping cells.
        Dim data As Variant
        data = searchRange.Value
        
        ' Loop every row in the array.
        Dim rowIndex As Long
        For rowIndex = LBound(data, 1) To UBound(data, 1)
            
            ' Loop every column in the array.
            Dim columnIndex As Long
            For columnIndex = LBound(data, 2) To UBound(data, 2)
            
                ' If current row/column matches the correct value then return the range.
                If data(rowIndex, columnIndex) Like what Then
                    Set FindFast = searchRange.Cells(rowIndex, columnIndex)
                    Exit Function
                End If
            Next columnIndex
        Next rowIndex
        
        ' If the range is not found then `Nothing` is returned.
        Set FindFast = Nothing
    End Function
    

    【讨论】:

    • 没有直接关系,但是我在哪里可以找到有关您设置“文档字符串”方式的更多信息?我很乐意采用与此类似的东西,如果存在某种“规范”,我会喜欢工作。
    • @AlexPeters,我在模仿 JS Doc — 这是 JavaScript 的主要文档样式。我确实有一个Style Guide 关于我如何编写我的 VBA;尽管它可能不是 100% 完整或最新的。
    • 不幸的是,Range.Find 方法没有提供覆盖其默认值以包含隐藏单元格的选项。但这是一个极好的、有用的解决方法。谢谢!
    • 一个建议是通过将 'what' 参数声明为 Variant 来使其更通用。
    【解决方案3】:

    真的有必要在宏里面做,会更容易使用匹配:

    =MATCH(G9;H9:H16;0)
    

    G9 : DropDownBox 的单元格

    H9:H16 : 你的范围

    0 : 完全匹配

    返回数组内的索引

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多