+ Reply to Thread
Results 1 to 16 of 16

Copy Row to Separate Sheet if Cell Contains Certain Text

  1. #1
    Registered User
    Join Date
    01-07-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    92

    Question Copy Row to Separate Sheet if Cell Contains Certain Text

    Hi
    I've been trying very unsuccessfully to write some existing code which will copy rows of information if a specific cell in that row contains some specific characters. If a cell in column E contains the "P1" anywhere in a string of text I need to copy the whole row to another sheet. Happy to share the code I have or an example sheet if people think it will be useful.
    Thanks in advance.
    Jay
    Last edited by jsolder; 01-12-2017 at 09:13 AM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    One way
    Run from sheet1 in attached workbook with {CTRL} + t
    Results put in sheet named "Results"

    Please Login or Register  to view this content.
    You could also put this formula into one of the columns (see attached workbook column G) and then filter on that column - all values > 0 include P1 in column E
    Put formula in cell G2 and copy down
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by kev_; 01-11-2017 at 01:14 PM.

  3. #3
    Registered User
    Join Date
    01-07-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    92

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    Thanks Kev. What part of the code would I need to modify if I wanted it to copy everything up to column AN rather than column G? I'm just trying to understand what each part of the code is doing. Thanks, Jay

  4. #4
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    You are welcome

    You do not need to alter anything

    The code works by hiding rows that do not meet the criteria with:
    Please Login or Register  to view this content.
    Then it copies all the non-hidden cells in the sheet with:
    Please Login or Register  to view this content.
    It makes no difference how wide the row is.

  5. #5
    Registered User
    Join Date
    01-07-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    92

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    Oh great, that's brilliant then!

  6. #6
    Registered User
    Join Date
    01-07-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    92

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    Sorry Kev, I think I spoke too soon! Everytime I try and run the Macro with my data set I get the following error: "Excel cannot complete this task with available resources. Choose less data or close other applications.", the i get a run-time error '1004' Copy method of Range class failed. When i click on debug it highlights this line of code:

    Please Login or Register  to view this content.
    Any ideas...?!

  7. #7
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    One thing for you to note:

    Everything relies on this line
    Please Login or Register  to view this content.
    VBA looks right and down from A1 until it finds an empty column in one direction and an empty row in the other to define the "CurrentRegion" range


    It means that your headings must be in row 1, that your data starts in row 2, that there are no blank columns or blank rows in the data (ie data must be contiguous)

  8. #8
    Registered User
    Join Date
    01-07-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    92

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    Is there an alternative way of defining the "CurrentRegion" range, as there will be some blank columns within the data, however there will never be a blank row until the end of the data set.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,597

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    Quote Originally Posted by jsolder View Post
    Happy to share the code I have or an example sheet if people think it will be useful.
    Thanks in advance.
    Jay
    Yes, it will be and I feel Filter would be the best method in this case.

  10. #10
    Registered User
    Join Date
    01-07-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    92

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    Hi, see example attached
    Attached Files Attached Files

  11. #11
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    Run the macro in the attached workbook with {CTRL} + t

    As requested range determination has been amended as follows:

    - VBA looks leftwards from the last column of the worksheet until it finds an occupied cell in row1 (=last column)
    - VBA looks upwards from the last row of the worksheet until it finds an occupied cell in Column "A" (=last row)

    So that means that your last column must have a header in row1 and your last row must have a value in column "A")
    Will this always be the case?

    I have also reduced the strain on memory by amending the copy/paste line to
    Please Login or Register  to view this content.
    This means that it is only copying the columns in the data range.
    I had been lazy and (by using .cells as the original range) was asking VBA to copy all the way across to column "XFD", which made your computer splutter!

    Test database expanded to include blank columns "H" and "M"
    Formula =IFERROR(SEARCH("P1",E2),"") is included in column "AA" simply allow me to check the results quickly.


    Please Login or Register  to view this content.
    Attached Files Attached Files

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,597

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    Here's how I do
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    01-07-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    92

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    Thanks guys, both of these work! Is there any advantage to using one over the other?

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,597

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    Filter is faster and code is easier, shorter in this case.

  15. #15
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    You are welcome.
    Thanks for the reps

  16. #16
    Registered User
    Join Date
    07-17-2018
    Location
    KY
    MS-Off Ver
    2016
    Posts
    4

    Re: Copy Row to Separate Sheet if Cell Contains Certain Text

    Quote Originally Posted by jindon View Post
    Here's how I do
    Please Login or Register  to view this content.
    Hi Jindon,

    I am trying to do something similar to the OP.

    I would like to search my first sheet "MP List" in columns C, E and F for two pieces of text "cost" and "cc" and if they exist in any cell in those 3 columns, copy the entire row to a new sheet called "Cost" and delete them from the original sheet.

    Could you help me understand your code so I can accomplish that?

+ 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: 2
    Last Post: 06-01-2015, 05:25 AM
  2. Text and numbers in one cell to reference in another cell on a separate sheet
    By meltshop85 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-25-2014, 12:56 AM
  3. Replies: 1
    Last Post: 08-22-2014, 08:25 AM
  4. Using macro to locate certain text, then copy the cell below text into another sheet
    By BobertSama in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-12-2013, 01:32 PM
  5. Replies: 0
    Last Post: 09-25-2012, 02:31 PM
  6. Replies: 3
    Last Post: 08-16-2012, 03:25 PM
  7. Replies: 24
    Last Post: 01-17-2012, 12:06 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