+ Reply to Thread
Results 1 to 6 of 6

Macro to copy *PART* of a row into another worksheet if multiple criteria are met

  1. #1
    Registered User
    Join Date
    01-20-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    3

    Macro to copy *PART* of a row into another worksheet if multiple criteria are met

    I'm creating a database for a research study that has a "master sheet" with all the details for every study participant.

    The participants are listed in rows, with their details in multiple columns.

    The study participants all belong to 9 subgroups which are defined by two of the columns (e.g. X and Y), whose values can be between 1 and 3.

    I would like to have a macro that runs through the master sheet and copies part of the row (e.g. columns A-M) for each participant into a separate work sheet based on their subgroup defined by columns X and Y.

    For example:
    - If on row 2 of the master sheet x2=1 and y2=1, then copy a2:m2 to the next empty line in worksheet 2 (starting from row 2)
    - If on row 3 of the master sheet, x3=1 and y3=1, then compy a3:m3 to the next empty line in worksheet 3 (starting from row 3)

    It is important to just copy part of the row, not the entire row, because there is information to the right of the main dataset in each worksheet that I do not want modified.

    I have the following code which will copy the entire row into sheet 2 based on a single criteria in column x:

    Sub copyrows()

    Dim tfCol As Range, Cell As Object

    Set tfCol = Range("x2:x1000")

    For Each Cell In tfCol

    If IsEmpty(Cell) Then
    Exit Sub
    End If

    If Cell.Value = "1" Then
    Cell.EntireRow.Copy
    Sheet2.Select
    ActiveSheet.Range("A65536").End(xlUp).Select
    Selection.Offset(1, 0).Select
    ActiveSheet.Paste
    End If

    Next

    End Sub

    This works well, but it copies the entire row, not just columns A-M, and it only uses one variable. Is it possible to modify this code to perform the task I need, or will I require a different approach.

    My experience with visual basic is extremely limited so as detailed help as possible would be very much appreciated. I have tried extensive searches on forums but can't quite find code that suits my problem.

    Many thanks in advance for any help.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,477

    Re: Macro to copy *PART* of a row into another worksheet if multiple criteria are met

    It should be easy enough to do that. If you post a small sample workbook, someone will provide suitable code.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    01-20-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Macro to copy *PART* of a row into another worksheet if multiple criteria are met

    Thanks for quick response. I've attached an example workbook.

    In this example, the two columns that decide a persons subgroup are L and M. The value of L can be "NSP", "Sup" or "Ad", while the value of M can be from 0-12. The 9 subgroups will be defined as:
    - Group 1: L="NSP", M<5
    - Group 2: L="NSP", M=5-8
    - Group 3: L="NSP", M>8
    - Group 4: L="sup", M<5
    - Group 5: L="sup", M=5-8
    - Group 6: L="sup", M>8
    - Group 7: L="ad", M<5
    - Group 8: L="ad", M=5-8
    - Group 9: L="ad", M>8

    I would like a macro to copy columns A-M only and paste them into 9 new worksheets according to their subgroup as defined by column L and M.

    Again, thanks for any help.
    Attached Files Attached Files

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,477

    Re: Macro to copy *PART* of a row into another worksheet if multiple criteria are met

    This copies just columns A to M for each selected row:

    Please Login or Register  to view this content.

    Regards, TMS

  5. #5
    Registered User
    Join Date
    01-20-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Macro to copy *PART* of a row into another worksheet if multiple criteria are met

    Thanks very much for your help. Solved the problem completely.

    Isn't the internet a wonderful thing?

    A

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,477

    Re: Macro to copy *PART* of a row into another worksheet if multiple criteria are met

    You're welcome. Thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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