Results 1 to 6 of 6

Thread: find (xp)

  1. #1
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Kitchener, Ontario, Canada
    Posts
    228
    Thanks
    0
    Thanked 0 Times in 0 Posts

    find (xp)

    I have a column of text. what I need to do is search the column and find a certian text string, keep searching down the same column to find a second different text string and then select all the information in between and copy it to a different wooksheet.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: find (xp)

    Could you attach a workbook that illustrates your question, with an indication of which column and which strings are involved?

  3. #3
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Kitchener, Ontario, Canada
    Posts
    228
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: find (xp)

    here is the idea

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: find (xp)

    Try this code. If it does not do exactly what you want, you may be able to adapt it to your needs.

    Option Explicit

    Sub Extract()
    Const strCol = "B"
    Const strFirst = "This is the first line I want to look for"
    Const strLast = "This is the last line I want"
    Dim rngFirst As Range
    Dim rngLast As Range
    Dim wshCur As Worksheet
    Dim wshNew As Worksheet

    Set rngFirst = Range(strCol & ":" & strCol).Find( _
    What:=strFirst, LookIn:=xlValues, LookAt:=xlWhole)
    If rngFirst Is Nothing Then
    MsgBox "First text not found.", vbExclamation
    Exit Sub
    End If

    Set rngLast = Range(strCol & ":" & strCol).Find( _
    What:=strLast, LookIn:=xlValues, LookAt:=xlWhole, After:=rngFirst)
    If rngLast Is Nothing Then
    MsgBox "Last text not found.", vbExclamation
    Exit Sub
    End If

    Set wshCur = ActiveSheet
    Set wshNew = Worksheets.Add
    wshCur.Range(rngFirst, rngLast).Copy wshNew.Range(strCol & "1")
    Set wshNew = Nothing
    Set wshCur = Nothing
    Set rngLast = Nothing
    Set rngFirst = Nothing
    End Sub

  5. #5
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Kitchener, Ontario, Canada
    Posts
    228
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: find (xp)

    That works great but how do I select the cell before rngLast as that is the header for the next group of info that I am trying to pull out.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: find (xp)

    rngLast.Offset(-1, 0) is the cell above rngLast. The arguments (-1, 0) mean -1 row down, i.e. 1 row up, and 0 columns to the right.

Posting Permissions

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