哪个属性保存在非连续单元格的结果范围内的项目?

时间:2010-07-14 09:33:52

标签: excel vba

我使用了OzGrid中的Find_Range函数,该函数返回一个带有找到项的Range对象。它成功地使用了它。我知道循环结果范围的每个项目允许更新/修改单个单元格。 Count属性显示正确的值。

没有Value属性,Value2属性只显示找到的第一个项目。

我的问题是 - Find_Range(一系列非连续单元格)的结果中的属性是什么,它列出了所有找到的项目?

修改

更清晰 - 通常Range对象(如ffg)可以提供范围中所有选定项的Variant数组

   Dim selRange as Range
   Dim vals as Variant 
   Set selRange = Range("A1:B10")
   vals = selRange.Value // 2D array with all values from the range

然而,

   Set selRange = Range("A1,A2,B10") // this is similar to the result of the Find-Range Function
   vals = selRange.Value // will only provide the value of A1 and not all three

Function Find_Range(Find_Item As Variant, _ 
    Search_Range As Range, _ 
    Optional LookIn As Variant, _ 
    Optional LookAt As Variant, _ 
    Optional MatchCase As Boolean) As Range 

    Dim c As Range 
    If IsMissing(LookIn) Then LookIn = xlValues 'xlFormulas
    If IsMissing(LookAt) Then LookAt = xlPart 'xlWhole
    If IsMissing(MatchCase) Then MatchCase = False 

    With Search_Range 
        Set c = .Find( _ 
        What:=Find_Item, _ 
        LookIn:=LookIn, _ 
        LookAt:=LookAt, _ 
        SearchOrder:=xlByRows, _ 
        SearchDirection:=xlNext, _ 
        MatchCase:=MatchCase, _ 
        SearchFormat:=False) 
        If Not c Is Nothing Then 
            Set Find_Range = c 
            firstAddress = c.Address 
            Do 
                Set Find_Range = Union(Find_Range, c) 
                Set c = .FindNext(c) 
            Loop While Not c Is Nothing And c.Address <> firstAddress 
        End If 
    End With 

End Function

1 个答案:

答案 0 :(得分:2)

不是100%肯定你的意思,但枚举结果似乎有效:

Dim searchTerm As String
searchTerm = "Search word"

Dim results As Excel.Range
Set results = Find_Range(Find_Item:=searchTerm, Search_Range:=ActiveSheet.Cells)

If Not results Is Nothing Then

    Dim cell As Excel.Range
    For Each cell In results
        Debug.Print cell.Value
    Next cell

End If

当你在For Each循环中枚举结果时,它隐式调用Range对象的.Cells-property,所​​以

For Each cell In results

相同
For Each cell In results.Cells

因为Find_Range返回多个单元格的范围,所以没有单个属性可以将结果作为列表提供。你必须循环遍历范围并自己构建列表。