views:

48

answers:

1

I have used the Find_Range function from OzGrid which returns a Range object with the found items. It have used it successfully. I know that looping through each item of the resultant Range allows for updates/modifications to the individual cells. The Count property shows the right value.

There is no Value property and the Value2 property only shows the first item found.

My question is - What is the property in the result of Find_Range ( which is a range of non-contiguous cells) that lists all the found items?

Edit:

A bit more clarity - Generally a Range object such as the ffg can provide a Variant array of all the selected items in a Range

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

However,

   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
+2  A: 

Not 100% sure what you mean, but enumerating the results seems to work:

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

When you enumerate the results in the For Each loop, it implicitly calls the Range object's .Cells-property, so

For Each cell In results

is the same as

For Each cell In results.Cells

Because the Find_Range returns a range of multiple cells, there is no single property that can give the results as a list. You have to loop through the range and build the list yourself.

fencliff
But doesn't any range also consist of multiple cells?
Ahmad
@Ahmad: Any range can consist of multiple cells. However Range is the data type we use to refer to a single cell as well - a range of one. If you try to call Range.Value on a range that contains more than one cell, you get an error. I wasn't sure what you were asking for in your OP. Did I answer your question?
fencliff
@fencliff - if I am not mistaken, a range of contiguous cell's Value property will return a variant array
Ahmad
@Ahmad - you're absolutely right. It's been a ... while since I've done any VBA. Anyway, I'm not sure I understand your original question then.
fencliff