+ Reply to Thread
Results 1 to 3 of 3

Keyword search and copy row to new sheet: search term problem

  1. #1
    Registered User
    Join Date
    11-20-2014
    Location
    Nairobi, Kenya
    MS-Off Ver
    2013
    Posts
    2

    Keyword search and copy row to new sheet: search term problem

    Hi guys,

    So I created the following code to act as a search function for keywords in an open sheet. When it finds that word, it copies the entire row to another sheet. Unfortunately at the moment it only works if a word is on it's own in a cell (rather than in a sentence for example). Can someone help to fix this so that it also searches for, and copies, rows with that word anywhere in the text?

    P.s. some of the code might be redundant because I'm teaching myself and I have been playing around with it for a while.

    Option Explicit
    Sub CopyPaste()
    Application.ScreenUpdating = True
    Dim vSearch As Variant
    vSearch = InputBox("Search")

    'this variable holds a search phrase, declared as variant as it might be text or number

    Dim xRow&, NextRow&, LastRow&
    NextRow = 2
    LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    'the macro prompts a user to enter the search phrase
    For xRow = 1 To LastRow

    If WorksheetFunction.CountIf(Rows(xRow), vSearch) > 0 Then
    Rows(xRow).Copy Sheets("SearchResults").Rows(NextRow)
    NextRow = NextRow + 1
    End If
    Next xRow
    Application.ScreenUpdating = True
    End Sub

  2. #2
    Registered User
    Join Date
    07-03-2013
    Location
    Montreal, QC
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: Keyword search and copy row to new sheet: search term problem

    I think you just need a minor tweak to your code.

    If WorksheetFunction.CountIf(Rows(xRow), "*" & vSearch & "*") > 0 Then

    Try This

  3. #3
    Registered User
    Join Date
    11-20-2014
    Location
    Nairobi, Kenya
    MS-Off Ver
    2013
    Posts
    2

    Re: Keyword search and copy row to new sheet: search term problem

    That's super - 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)

Similar Threads

  1. Replies: 6
    Last Post: 03-18-2014, 11:16 AM
  2. Replies: 2
    Last Post: 03-17-2014, 04:19 PM
  3. Re-useability:Search a keyword and copy adjacent cell to in another sheet
    By illustratum in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-21-2013, 06:54 PM
  4. [SOLVED] Keyword search, copy rows to new sheet. Results far from consistent
    By Smatths in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-01-2011, 11:11 AM
  5. Keyword search and copy cell below to new sheet...
    By pixelsnaps in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-14-2010, 03:30 PM

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