+ Reply to Thread
Results 1 to 7 of 7

Thread: Using the Find Function and Multiple Offset Columns

  1. #1
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    71

    Using the Find Function and Multiple Offset Columns

    Hey Everyone,

    I'm kinda of new to VB and you have all been so helpful. Currently I've been spending the paste little while writing a VB Excel Code that will search a worksheet for specific text ("cat") , once it finds the location of said text it copies the next cell over [Offset(, 1)] into another worksheet (Sheet2). Anywho in my actual code once I find the row I'm looking for I have to copy several columns from that row. So, I'm just curious is there a more efficient way to do this than to use the "Find" function every time? I mean I already know the row I'm looking for, so should I do the entire line of code again just to change the offset to (, 2). and again and again?

    Below is the sample code I'm working with

    Sub FindCode()
        Worksheets("Sheet2").Cells(1, "B").Value = Cells.Find(What:="cat", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
            xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False).Offset(, 1).Value
    End Sub
    Thanks again everyone for your help!
    Last edited by ashleys.nl; 09-13-2011 at 12:14 PM. Reason: Solved

  2. #2
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: Using the Find Function and Multiple Offset Columns

    Also, how do I look through the whole worksheet not just after the ActiveCell?

    Thanks again

  3. #3
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Using the Find Function and Multiple Offset Columns

    A better way is to assign a range variable to the Find and then you can also check the value is found and avoid an error if not:
    Sub FindCode()
        
    Dim r As Range
        Set r = Cells.Find(What:="cat", LookIn:=xlFormulas, LookAt:=xlWhole, _
                           SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
                           SearchFormat:=False)
        If Not r Is Nothing Then
            Worksheets("Sheet2").Cells(1, "B").Value = rfind.Offset(, 1)
            Worksheets("Sheet2").Cells(1, "C").Value = rfind.Offset(, 2)
            'etc
        End If
        
    End Sub

  4. #4
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: Using the Find Function and Multiple Offset Columns

    Hey Stephen,

    That worked great, except I had to change "rfind.Offset...." to "r.Offset". "rfind" wasn't an object. But other than that it works great! thank you soo much for all your help lately. Below is the code I'm using now for others to see.

    Sub FindCode()
      
    Dim r As Range
        
        Set r = Worksheets("Sheet1").Cells.Find(What:="cat", LookIn:=xlFormulas, LookAt:=xlWhole, _
                           SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
                           SearchFormat:=False)
        If Not r Is Nothing Then
            Worksheets("Sheet2").Cells(1, "B").Value = r.Offset(, 1).Value
            Worksheets("Sheet2").Cells(1, "C").Value = r.Offset(, 2).Value
            'etc
        End If
        
    End Sub

  5. #5
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: Using the Find Function and Multiple Offset Columns

    Hey, Just when I think I have something solved I have more work to do....

    So when I try to implement this into my master work book, I realize that my search term "Cat" appears 3 times and at each occurance there is useful information in that row that I want to take away. Is there a way to implement the code so that I could have multiple "r" values of "r1", "r2", and "r3"? Each value of course refering to their respective order of occurence.

    Thanks again in advance

  6. #6
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: Using the Find Function and Multiple Offset Columns

    Quote Originally Posted by ashleys.nl View Post
    Hey, Just when I think I have something solved I have more work to do....

    So when I try to implement this into my master work book, I realize that my search term "Cat" appears 3 times and at each occurance there is useful information in that row that I want to take away. Is there a way to implement the code so that I could have multiple "r" values of "r1", "r2", and "r3"? Each value of course refering to their respective order of occurence.

    Thanks again in advance
    Currently I'm using this code , but with no luck. It finds the first occurence of "cat" twice rather than moving on to the next.

    any thoughts???

    Sub FindCode()
      
    Dim r As Range
    Dim r2 As Range
        
        Set r = Worksheets("Sheet1").Cells.Find(What:="cat", LookIn:=xlFormulas, LookAt:=xlWhole, _
                           SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
                           SearchFormat:=False)
        Set r2 = Worksheets("Sheet1").Cells.FindNext
        If Not r Is Nothing Then
            Worksheets("Sheet2").Cells(1, "B").Value = r.Offset(, 1).Value
            Worksheets("Sheet2").Cells(1, "C").Value = r.Offset(, 2).Value
            Worksheets("Sheet2").Cells(2, "B").Value = r2.Offset(, 1).Value
            Worksheets("Sheet2").Cells(2, "C").Value = r2.Offset(, 2).Value
        End If
        
    End Sub

  7. #7
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Using the Find Function and Multiple Offset Columns

    Try this:
    Sub FindCode()
      
    Dim r As Range, s As String, n As Long
        
    Set r = Worksheets("Sheet1").Cells.Find(What:="cat", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    
    If Not r Is Nothing Then
        s = r.Address
        Do
            n = n + 1
            Worksheets("Sheet2").Cells(n, "B").Value = r.Offset(, 1).Value
            Worksheets("Sheet2").Cells(n, "C").Value = r.Offset(, 2).Value
            Set r = Worksheets("Sheet1").Cells.FindNext(r)
        Loop While r.Address <> s
    End If
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0