+ Reply to Thread
Results 1 to 3 of 3

A Macro that will find all occurance of a keyword and post to new sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    01-11-2013
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    2

    A Macro that will find all occurance of a keyword and post to new sheet

    I have a workbook that is 62000 rows long and contains lots of information including numbers and text. I have been searching all day, and can't seem to search the right phrase to get the answer I need, so please forgive me if this is elsewhere. Basically I would like a macro to search for ALL occurrences of the four different numbers and copy the entire row onto a new sheet. I have found examples that will copy it once, or all occurrences in a specific column, but my numbers could be in any column and I need every time that any of the four different numbers would appear to be copied to one single sheet separate from the rest of the information. ANY HELP APPRECIATED! I have attached an example sheet, that does not use the correct numbers. I'm very VERY knew to this so please use small words with me

    So, in the example I would need every time the number 1234567891 appeared anywhere in the sheet, the entire row to be copied to a new sheet except in real life I have four different numbers to search for. example.xls

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: A Macro that will find all occurance of a keyword and post to new sheet

    You left a lot of ambiguity in your description. Try something like this.

    Sub Copy_Matched_KeyWord_Rows()
        
        
        Dim ws1 As Worksheet, ws2 As Worksheet
        Dim Found As Range, vKeyWords As Variant, i As Long
        
        Set ws1 = Sheets("Sheet1")  'Data sheet
        Set ws2 = Sheets("Sheet2")  'Destination
        
        vKeyWords = Array("1234567891")
        'Additional vKeyWords like this.
        'vKeyWords = Array("1234567891", "1234567892", "1234567893", "99999999")
        
        For i = LBound(vKeyWords) To UBound(vKeyWords)
            Set Found = ws1.Cells.Find(What:=vKeyWords(i), _
                                       LookIn:=xlValues, _
                                       LookAt:=xlWhole, _
                                       SearchOrder:=xlByRows, _
                                       SearchDirection:=xlNext, _
                                       MatchCase:=False)
            Do While Not Found Is Nothing
                Found.EntireRow.Copy Destination:=ws2.Range("A" & Rows.Count).End(xlUp).Offset(1)
                Found.EntireRow.Hidden = True   'Prevent finding this row again
                Set Found = ws1.Cells.FindNext(After:=Found)
            Loop
        Next i
        ws1.Rows.Hidden = False
        
    End Sub

  3. #3
    Registered User
    Join Date
    01-11-2013
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    2

    Re: A Macro that will find all occurance of a keyword and post to new sheet

    PERFECT!!! Thank you so much! Sorry for the ambiguity, I'm working with confidential information and I was really nervous about disclosing anything privileged. Thank you so much though you saved me probably days of work!

+ Reply to Thread

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