Consulting

Results 1 to 9 of 9

Thread: Solved: 'find a range' problem

  1. #1

    Solved: 'find a range' problem

    Relative newbie ....
    Striving to code the finding of a range within a range. The return always seems to be the first match to 'F1', ignoring the rest of the range.

    Can someone PLEASE show me where I am going wrong.

    [VBA]
    Set c = Range("A1:C30").Find(Range("F1:H1").Value, LookIn:=xlValues, LookAt:=xlWhole)
    Range("G12").Value = c.Row
    [/VBA]

    thnx
    Remalay

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    You can't use code like that to find multiple values within a range.

    What exactly are you trying to do?

  3. #3
    OK. Thanks for that - someone has mis-informed me.... ah well.
    I'm trying to find a unique matching row of data (colums A thru F) from a range of 2,000+ records, in order to retrieve the associated data from adjacent columns.

  4. #4
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    maybe you can concatenate all in one cell the values of the cells of each row and look for that unique value

  5. #5
    Thanks for that ALe, an option I might be forced to employ if a 'neater' solution is not forthcoming (I find it hard to believe there isn't one).

    rgds
    remalay

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A slight modification of the FindNext method should suffice
    [VBA]
    Sub FindGroup()
    Dim ToFind As Range, Found As Range, c As Range
    Dim FirstAddress As String
    Set ToFind = Range("E3:E5")
    With Worksheets(1).Range("a1:a500")
    Set c = .Find(ToFind(1), LookIn:=xlValues)
    If Not c Is Nothing Then
    FirstAddress = c.Address
    Do
    If c.Offset(1) = ToFind(2) And c.Offset(2) = ToFind(3) Then
    Set Found = Range(c, c.Offset(2))
    GoTo Exits
    End If
    Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address <> FirstAddress
    End If
    End With
    Exits:
    MsgBox Found.Address
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Just noticed you're working with rows. Just switch around the c.offset to column offsets and you should be OK. Only search Column A for the initial value, not Columns A-C as your first code.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Thanks mdmackillop, it works a treat.

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Glad to help.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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