+ Reply to Thread
Results 1 to 6 of 6

Need a Macro to search for text in sheet and copy the matching row to new sheet

Hybrid View

robin_108 Need a Macro to search for... 10-03-2012, 02:10 PM
Bishonen Re: Need a Macro to search... 10-03-2012, 02:43 PM
robin_108 Re: Need a Macro to search... 10-03-2012, 10:25 PM
robin_108 Re: Need a Macro to search... 10-04-2012, 12:25 PM
Bishonen Re: Need a Macro to search... 10-04-2012, 01:35 PM
robin_108 Re: Need a Macro to search... 10-08-2012, 12:59 PM
  1. #1
    Registered User
    Join Date
    10-03-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    9

    Question Need a Macro to search for text in sheet and copy the matching row to new sheet

    Hi I am New to Excel macro scripting . I need your help to find do a task in excel which i am struggling till now .

    I have excel in which sheet1 & sheet2 contents are as attached in excel

    I want to script which reads the first column of sheet2 until the end of empty cell and then find each cell value against sheet1 and print all rows which has the value of cell to sheet3 . so as per the excel sheet attached say sheet2.A1=luke .I have to find for luke in sheet1 ,Now I find 2nd row and 3rd row has contents of luke and script has to copy the entire matched rows contents to sheet3 . next it has to take sheet2.A2 value "nick" and do the same find operation in sheet and copy the matched row1 and append it sheet3 contents.

    Thanks in Advance.

    Regards
    Rob
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Need a Macro to search for text in sheet and copy the matching row to new sheet

    do you wish to look for luke, nick and whatever else might reside in the first column of sheet2 in column "F" of the first row? Or should the search take place on the whole sheet? Are we looking for cells that have the exact same content or cells that have the searched phrase within them(like C2 = this is Luke testfile?

  3. #3
    Registered User
    Join Date
    10-03-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Need a Macro to search for text in sheet and copy the matching row to new sheet

    Search should happen on the whole sheet1 and the search phrase can be within the cell content like c2 "this is Luke testfile" .

  4. #4
    Registered User
    Join Date
    10-03-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Need a Macro to search for text in sheet and copy the matching row to new sheet

    I managed to write the macro on my own but the processing time is more since the records are more.

    Is there a way I can find all the cell adresses for a matched string and set it to a range variable. ?

    suppose If i can see the matched string on cell address A4,A10,A18 can I set a range to "A4,A10,A18" in the macro ? If so how do i set it and what is the syntax .Apologies if you cannot understand my question since i am new to macro terms.

    Thanks in Advance!
    Rob

  5. #5
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Need a Macro to search for text in sheet and copy the matching row to new sheet

    here's something that should get you started. Please be aware that the code is far from being optimal.

    It looks for the value "2" in the first column. The address of every cell which contains "2" will be stored in an array (lim). At the end lim is being displayed.

    I'd really love someone to shorten this to the minimum so i could learn a bit too :-)

    Sub Sample()
        Dim oRange As Range, aCell As Range
        Dim ws As Worksheet
        Dim ExitLoop As Boolean
        Dim SearchString As String, FoundAt As String, lim() As Variant, i As Integer, k As Integer
        Set ws = Worksheets(1)
        Set oRange = ws.Columns(1)
        i = 0
        k = 0
    
        SearchString = "2"
        Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
        If Not aCell Is Nothing Then
        aCell.Activate
        
        ReDim lim(0 To 20)
            lim(0) = aCell.Address
            i = i + 1
            Do While ExitLoop = False
                Set aCell = oRange.FindNext(After:=aCell)
    
                If Not aCell Is Nothing Then
                aCell.Activate
                    If aCell.Address = lim(0) Then Exit Do
                    k = k + 1
                    ReDim Preserve lim(0 To k)
                    lim(i) = aCell.Address
                    i = i + 1
                Else
                    ExitLoop = True
                End If
            Loop
        Else
            MsgBox SearchString & " not Found"
        End If
        
        For i = 0 To UBound(lim)
        MsgBox (lim(i))
        Next
    
    End Sub
    Last edited by Bishonen; 10-04-2012 at 01:38 PM.

  6. #6
    Registered User
    Join Date
    10-03-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Need a Macro to search for text in sheet and copy the matching row to new sheet

    Thanks Bishonen !

    "FindNext" did the trick for me . It reduced the time of execution as well I was able to achieve what i was expecting .

    Thanks a lot

+ 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.6.0 RC 1