+ Reply to Thread
Results 1 to 8 of 8

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

Hybrid View

mea02300 Search document for rows... 07-08-2013, 04:14 PM
LaffyAffy13 Re: Search document for rows... 07-08-2013, 04:35 PM
LaffyAffy13 Re: Search document for rows... 07-08-2013, 04:37 PM
tigeravatar Re: Search document for rows... 07-08-2013, 04:40 PM
stnkynts Re: Search document for rows... 07-08-2013, 04:51 PM
tigeravatar Re: Search document for rows... 07-08-2013, 05:08 PM
macronewbie9981 Re: Search document for rows... 07-08-2013, 05:10 PM
mea02300 Re: Search document for rows... 07-09-2013, 08:44 AM
  1. #1
    Registered User
    Join Date
    06-24-2013
    Location
    Coal Township
    MS-Off Ver
    Excel 2010
    Posts
    8

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

    I am attempting to sort data on a very large report, but I am very inexperienced with the use of VBA.

    In sheet 1 of this report are several columns of data. What I am looking to do is get a macro that will search for a specific word in either column A OR column F, then cut/paste the entire row containing that word onto sheet 2 and remove the blank row.

    Things to consider:
    1. Columns A and F will contain other information than just the word I am looking for.
    2. There may be, at times, a blank cell in column F
    3. The report is almost 7000 rows.
    4. The word may appear in BOTH column A AND column F in some rows.
    5. The word may appear with both capital and lower-case letters

    For instance, I may want to take any row containing the word "diagnosis" and cut/paste that entire row onto sheet 2. The word "diagnosis" may appear in 100 different rows and may appear in the same row more than once. The word "diagnosis" may also appear as "Diagnosis" or "DIAGNOSIS".

    I'm not sure if I left anything out. I appreciate any help that anyone can provide. If there are any additional concerns, please let me know and I will respond ASAP. Thanks again.
    Last edited by mea02300; 07-08-2013 at 04:17 PM. Reason: Correct information

  2. #2
    Forum Contributor
    Join Date
    07-24-2012
    Location
    Denver, CO
    MS-Off Ver
    365
    Posts
    230

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

    Don't know if this will work, so save the file you already have under a new name to keep your old information safe. I also don't know if this will also grab the words that have capital letters instead.

    
    Dim rng as Range
    Dim J as integer
    
    Sheets("Sheet1").Select
    For each rng in Range("A1:A7000")
    
    If rng.FormulaR1C1 = "diagnosis" Then
    
    rng.EntireRow.Select
    
    Selection.Copy
    
    Sheets("Sheet2").Select
    
    J= J+ 1
    Range("A" & J).Select
    
    ActiveSheet.Paste
    
    Sheets("Sheet1").Select
    rng.EntireRow.Delete
    
    End If
    
    Next rng

  3. #3
    Forum Contributor
    Join Date
    07-24-2012
    Location
    Denver, CO
    MS-Off Ver
    365
    Posts
    230

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

    Then for column F just put "F1:F7000" instead of "A1:A7000" for the range

  4. #4
    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

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

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

    You didn't specify how you are going to assign the "word" so I assumed an input box.

    Try this:

    Sub RunMe()
    Dim ws1 As Worksheet:   Set ws1 = Sheets("Sheet1")
    Dim ws2 As Worksheet:   Set ws2 = Sheets("Sheet2")
    Dim c As Range, myRange As Range
    Dim LR As Long
    Dim myValue As String
    Dim firstaddress As Variant
    
    On Error Resume Next
    Application.ScreenUpdating = False
    LR = ws1.Range("A" & Rows.Count).End(xlUp).Row
    myValue = InputBox("Type in the search value.")
    If myValue = "" Then Exit Sub
    
    Set myRange = Union(ws1.Range("A1:A" & LR), ws1.Range("F1:F" & LR))
    With myRange
        Set c = .Find(What:=myValue, LookIn:=xlValues, LookAt:=xlWhole)
        If Not c Is Nothing Then
            firstaddress = c.Address
            Do
                c.EntireRow.Cut ws2.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstaddress
        End If
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    
    End Sub
    Note: You might want to pull the Msgbox out of tigeravatar's code. It was there for testing purposes im sure.

  6. #6
    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

    Quote Originally Posted by stnkynts View Post
    Note: You might want to pull the Msgbox out of tigeravatar's code. It was there for testing purposes im sure.
    Thanks for spotting that! I forgot to delete it out. You are quite right, it was there for testing

  7. #7
    Registered User
    Join Date
    07-08-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    17

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

    I jsut asked this in another thread pretty much. Not sure how to link it but look at my history.

  8. #8
    Registered User
    Join Date
    06-24-2013
    Location
    Coal Township
    MS-Off Ver
    Excel 2010
    Posts
    8

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

    Thank you very much everyone. I removed the msgbox and the code worked perfectly. Not a single bug or issue to report. I really appreciate it. That was a huge help.

+ 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