Results 1 to 4 of 4

Find Range Function

Threaded View

smuzoen Find Range Function 07-05-2012, 09:43 AM
shg Re: Find Range Function 07-05-2012, 11:54 AM
GeneralDisarray Re: Find Range Function 07-05-2012, 02:05 PM
smuzoen Re: Find Range Function 07-05-2012, 07:07 PM
  1. #1
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Find Range Function

    I have used this so much I cannot do without it - it is a quick method to find all the ranges within a range than containing a value. The function is
    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 c.Address <> firstAddress
            End If
        End With
         
    End Function
    This can be called with
    Set myRange = Find_Range(myVar, Worksheets("Sheet1").Columns("A"), xlValues, xlWhole)
    Then simple matter of iterating through myRange to get all the ranges with the value. Could not live without this great function. Very robust.
    Last edited by smuzoen; 07-05-2012 at 08:45 PM. Reason: Amended code - correct error
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1