+ Reply to Thread
Results 1 to 10 of 10

Help! Need to move rows to separate worksheet if cell values match a value in range

  1. #1
    Registered User
    Join Date
    06-27-2011
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    6

    Help! Need to move rows to separate worksheet if cell values match a value in range

    I've browsed through the posts on this forum before and found a lot of other macros that have considerably eased my workload, so first off- thank you. I'm also hoping to get some assistance with a new macro that I'm writing (workbook attached.)

    To summarize, every month I do a massive data pull and dump a ton of unsorted information into a worksheet. Based on groups of product SKUs that belong to each of the "Packer", "Operator" and "Distributor" categories, I move each row of data into one of three other worksheets of same names. If a new product SKU appears, I have to determine what category it belongs to before I move that row to the appropriate worksheet.

    There are about ~1000 rows of data so, as you can imagine, this takes up a huge amount of time every month

    What I would like to do is build a macro that automates the process; i.e. moves a row to the "Operator" worksheet if the cell value in the "Product SKU" column matches one of the values in a named range of Operator product SKUs, to the "Distributor" worksheet if it matches one of the values in a named range of Distributor product SKUs. It would also be great if the named ranges were re-defined when I added a new SKU.

    Can anyone provide some guidance? I've hit a roadblock as I'm not sure how to move based on this type of criteria and would really appreciate some help!
    Attached Files Attached Files
    Last edited by nzamparo; 07-13-2011 at 11:53 AM.

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Move rows to separate worksheet if cell values match a value in named range

    This code ran without error when tested on your sample worksheet/book, although I did limit the loop iterations by deleting some cells on the Category Product SKU sheet.

    Note: there is no list for Packer Category SKU, and I assumed this list will follow the same pattern & column separation as the Distributor and Operator lists.

    This code could be improved and there is no error handling built in.

    Please Login or Register  to view this content.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Registered User
    Join Date
    06-27-2011
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Move rows to separate worksheet if cell values match a value in named range

    Palmetto- thank you much for your help, I have only one problem.

    When the code is run, it seems to have a problem with the undefined variables Sheet5 and Sheet6. How do I fix this?

  4. #4
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Move rows to separate worksheet if cell values match a value in named range

    The code was written based on your sample workbook, in which it ran without error.

    As I said, there is no error handling built in - it's just the essential code.
    If sheet5 and sheet6 do not exist in the real workbook then the code will fail.

    References to sheet5 and sheet6 are sheet code names. You may need to adjust these references if that is where the code is failing.

  5. #5
    Registered User
    Join Date
    06-27-2011
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Move rows to separate worksheet if cell values match a value in named range

    That link was helpful, thanks Palmetto.

    When I run the workbook with the code, however, I get a "Runtime error 9: Subscript out of range" error. Do you have any suggestions on how to fix this?
    Attached Files Attached Files

  6. #6
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Move rows to separate worksheet if cell values match a value in named range

    Mistake on my part. Since your original workbook did not contain criteria for Packer, I had to set the array variable to (2).

    Near the top of the code on the "Dim" line, change the Sh(2) to Sh(3),

    After changing this, using your last sample workbook, the code ran to completion w/o error, although there were no values copies to the Packer sheet - a manual check with match formula conforms no Packer SKU exist in the source sheet, which confirms the code did not miss any of them.

    BTW, the code should go into a standard module - not in the Thisworkbook module.

    Adding the Macro

    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time.
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.
    Last edited by Palmetto; 07-08-2011 at 05:33 PM.

  7. #7
    Registered User
    Join Date
    06-27-2011
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Move rows to separate worksheet if cell values match a value in named range

    Palmetto- many, many thanks for your help. It's working perfectly now!

    This saves me a ton of time, I really appreciate it.

  8. #8
    Registered User
    Join Date
    06-27-2011
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Move rows to separate worksheet if cell values match a value in named range

    Just a thought- is there a way to modify the code provided so that rows are CUT and pasted into the next available row on each sheet, and not COPIED and pasted?

    My thinking is that the above would help me to identify any new product SKUs in the massive data pull that have not been added to one of the Categories (Packer, Operator, Distributor), since they would be the only ones left on the Massive Data Pull sheet after the macro operation was performed.

    Any help appreciated, and a big thank you for all your help thus far (Palmetto!)

  9. #9
    Registered User
    Join Date
    06-27-2011
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Help! Need to move rows to separate worksheet if cell values match a value in ran

    Hi guys- any insight on the above? Your help would be greatly appreciated!

  10. #10
    Registered User
    Join Date
    04-02-2010
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: Help! Need to move rows to separate worksheet if cell values match a value in ran

    Hi,

    I don't have any coding experience but in looking for a solution to my problem I ran across yours. Another options could be to add a conditional formatting to the rows being copied out so that at least they are visually identified - albeit cutting them is a way better solution.

    i think that the 'cutting' would actually be done by telling the script to delete the row after it's cell value had been passed to the proper sheet - but that's just my uninformed take =)

    Good luck



    Jay

+ 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