+ Reply to Thread
Results 1 to 8 of 8

Search document for rows containing a specific word, cut/paste into new sheet

Hybrid View

  1. #1
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Search document for rows containing a specific word, cut/paste into new sheet

    mea02300,

    Give this a try:
    Sub tgr()
        
        Dim wsData As Worksheet
        Dim wsDest As Worksheet
        Dim rngFound As Range
        Dim rngCopy As Range
        Dim strFirst As String
        Dim strFind As String
        
        strFind = InputBox("Enter the keyword to be searched for in columns A and F:", "Find Text")
        If Len(strFind) = 0 Then Exit Sub   'Pressed cancel
        
        Set wsData = Sheets(1)
        Set wsDest = Sheets(2)
        
        Set rngFound = wsData.Range("A:A,F:F").Find(strFind, , xlValues, xlPart)
        If Not rngFound Is Nothing Then
            strFirst = rngFound.Address
            Set rngCopy = rngFound.EntireRow
            Do
                Set rngCopy = Union(rngCopy, rngFound.EntireRow)
                Set rngFound = wsData.Range("A:A,F:F").Find(strFind, rngFound, xlValues, xlPart)
            Loop While rngFound.Address <> strFirst
            rngCopy.EntireRow.Copy wsDest.Cells(Rows.Count, "A").End(xlUp).Offset(1)
            rngCopy.EntireRow.Delete xlShiftUp
        End If
        
        Set wsData = Nothing
        Set wsDest = Nothing
        Set rngFound = Nothing
        Set rngCopy = Nothing
        
    End Sub
    Last edited by tigeravatar; 07-08-2013 at 05:09 PM. Reason: Removed the msgbox from the code. Thanks for spotting that, stnkynts!
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

+ 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