+ Reply to Thread
Results 1 to 8 of 8

If cell = X then copy entire row to new worksheet named X

  1. #1
    Registered User
    Join Date
    07-21-2013
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    4

    If cell = X then copy entire row to new worksheet named X

    Hey guys. I'm brand new to the forum and brand new to VBA. I had never used a macro until 4 days ago. So I've made some great progress using other individual's code to suit my needs to format a report I generate and create tables based on the data. However I'm stuck on a problem right now and my research hasn't turned up a solution.

    Basically I need excel to search Column B, (Aisles) on my worksheet and create a new worksheet for every aisle. So if column B equals "01", then create a new worksheet titled 01, (or Aisle 01 if possible), and copy all aisle 01 rows to it. It would then create worksheets for aisles 02, 03, etc. There are 64 aisles, and 21,000 rows on this sheet. I've attached an example of the worksheet. I would appreciate any help on this. And detailed explanations are always welcome because I am trying to learn VBA for future application to different reports. Thank you in advance.
    Attached Files Attached Files

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: If cell = X then copy entire row to new worksheet named X

    A very common need.

    'SHEET1 TO MANY SHEETS
    Here's a macro for parsing rows of data from one sheet to many sheets named for the same values in a specific column.It not only can parse the rows, it can create the sheets if they are missing. There is a sample sheet there where you can test this out.


    The two edits I think you'll need to make are:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 12-27-2019 at 04:38 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    07-21-2013
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: If cell = X then copy entire row to new worksheet named X

    Thanks JB! I tried it on my example and it worked great, exactly as needed. However when running it on the full report I get a popup window that says:

    "Excel ran out of resources while attempting to calculate one or more formulas. As a result, these formulas cannot be evaluated."

    I have an i7 3770 processor and 12GBs of ram, so I can't imagine it's a problem with my computer. Any advice?

  4. #4
    Registered User
    Join Date
    07-21-2013
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: If cell = X then copy entire row to new worksheet named X

    Well everything was working great on the test sheet, now the macro gets caught up at these lines:

    ws.Range("A" & TitleRow + 1 & ":A" & LR).EntireRow.Copy _
    Sheets(MyArr(Itm) & "").Range("A" & Rows.Count).End(xlUp).Offset(1)

    Not sure what happened.

  5. #5
    Registered User
    Join Date
    07-21-2013
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: If cell = X then copy entire row to new worksheet named X

    Ok, I officially feel like an idiot. When I retried the code in my test doc I used the wrong code (the one to copy data to existing sheets). Sorry for any confusion.

    However my first problem, the one where excel is running out of resources, still exists. I'll try and stop adding posts unless I get asked a question.

  6. #6
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: If cell = X then copy entire row to new worksheet named X

    Daniel,

    Here's a question.
    How does this one work for you?
    Please Login or Register  to view this content.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: If cell = X then copy entire row to new worksheet named X

    @kalak: eek! Looping through 20k+ rows and copying one row at a time... (shudder)



    @Daniel, Perhaps Excel is running into the "Areas" limitation on a dataset that size. Any chance you can SORT that data prior to running the macro so all the like items in column B are adjacent? That might resolve the issue.


    Another idea is to make a COPY of this workbook, then on the copy, remove all the formulas:

    1) Highlight all data
    2) COPY
    3) PASTE SPECIAL > VALUES
    4) Now run the macro

  8. #8
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: If cell = X then copy entire row to new worksheet named X

    Quote Originally Posted by JBeaucaire View Post
    @kalak: eek! Looping through 20k+ rows and copying one row at a time... (shudder)
    Jerry,

    perhaps you might like to read the code, or even try it out before making that sort of negative and totally incorrect comment.

    You're actually quite wrong. It doesn't copy "one row at a time". It copies all relevant rows in one lot.

    And incidentally, it'll run significantly faster on any significant problem on these lines than anything you're likely to put up.

    If you claim otherwise, then do a timing test on some data that can be replicated, post your results, and prove me wrong if you're able.

    I try to help people on this forum, and having that sort of stuff poked at my codes without reason except (presumably) sloppy reading on your part helps nobody and does you personally no credit.

+ 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: 3
    Last Post: 08-07-2011, 09:56 AM
  2. Copy all Rows with a cell of value 'x' to a worksheet named 'x'
    By sauce1979 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-04-2011, 12:17 PM
  3. If a cell in a column (in any worksheet) = x, copy entire row
    By cooler20days in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-15-2011, 11:19 AM
  4. copy entire row to new worksheet based on the fifth charter in a cell
    By davidparkes in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-13-2011, 03:24 AM
  5. Replies: 2
    Last Post: 05-19-2005, 04:08 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