+ Reply to Thread
Results 1 to 7 of 7

Copy row from one worksheet to another with multiple column criteria

  1. #1
    Registered User
    Join Date
    01-11-2021
    Location
    Toronto, Ontario
    MS-Off Ver
    365
    Posts
    4

    Lightbulb Copy row from one worksheet to another with multiple column criteria

    Hello! I'm looking to copy an entire row from one worksheet (Sheet1) to another (Sheet2) based on multiple criteria.

    For example, in the image below, I need to be able to find all the "Green" fruits and then refine that further to only "Medium" Size. Then, I need to copy the entire row (as long as it has both criteria) into a separate worksheet.Capture.JPG

    This is a simplified example but currently my code looks something like this:

    Private Sub CommandButton_Click()
    Dim c As Range
    Dim j As Integer

    Dim Source As Worksheet
    Dim Target As Worksheet

    Set Source = ActiveWorkbook.Worksheets("Input") 'name of the actual worksheet
    Set Target = ActiveWorkbook.Worksheets("Output") 'name of the actual worksheet

    j = 13 'need it to start looking at row 13 of Sheet1
    For Each c In Source.Range("B1:B1000") 'needs to work for column B (in the simplified example, search every row in the column "Colour")
    If c = "Green" And source.range(c, 3) = "Medium" Then 'find all rows that have both Green (from column B) andMedium (from column c, i.e. 3)
    Source.Rows(c.Row).Copy Target.Rows(j) 'copy the rows that match the criteria above to a new worksheet (preferably row 7 if at all possible)
    j = j + 1 'repeat for next row!
    End If
    Next c
    End Sub

    Many many thanks in advance.

  2. #2
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Copy row from one worksheet to another with multiple column criteria

    Here is a slightly different approach to meet the same ends.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    01-11-2021
    Location
    Toronto, Ontario
    MS-Off Ver
    365
    Posts
    4

    Re: Copy row from one worksheet to another with multiple column criteria

    This is great! Thank you!!

    Is there a way to modify the code slightly so that it doesn't copy to row 1 of Sheet2? I have a header that I don't want overwritten.

  4. #4
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Copy row from one worksheet to another with multiple column criteria

    Sure, glad it works.

    Change

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-11-2021
    Location
    Toronto, Ontario
    MS-Off Ver
    365
    Posts
    4

    Re: Copy row from one worksheet to another with multiple column criteria

    One last complication/challenge. Once again I'd greatly appreciate your help.

    I have a screenshot of what my actual code / spreadsheet looks like attached.

    Basically, I have a userform where you can input:
    File Type / Property Details / Floor Type / Wall Type / Roof Type / Foundation Type / Miscellaneous

    This inputs the selected values to cells AA1 - AA7 of Sheet 2 and if I have the exact match for all of them, the code below works great (huge thanks to you).

    However, I need to to work so that if any input is left blank, let's say AA3, the filtering only applies to the range with values. In the same image, I have a couple examples.

    Attachment 712937

    The last column lists (from a dropdown using code) multiple additional features so for filtering in the last column, the code needs to work so that if the value in cell AA7 is anywhere in the cell, it retrieves the row.

  6. #6
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Copy row from one worksheet to another with multiple column criteria

    Your attachment didn't come through. Re-attaching it usually works. But it would be best if you could upload a working sample instead of a picture, based on your description, there may be more to it than a slight change.
    Last edited by maniacb; 01-12-2021 at 05:16 PM.

  7. #7
    Registered User
    Join Date
    01-11-2021
    Location
    Toronto, Ontario
    MS-Off Ver
    365
    Posts
    4

    Re: Copy row from one worksheet to another with multiple column criteria

    Hello, Please see attached. Thanks!
    Attached Images Attached Images
    Attached Files Attached Files

+ 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. How to copy rows based on multiple criteria from one worksheet to another ?
    By Caster in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-26-2015, 06:27 AM
  2. splitting multiple worksheet based on column criteria
    By sachin483 in forum Excel General
    Replies: 2
    Last Post: 04-10-2015, 10:27 PM
  3. [SOLVED] Copy column data from multiple worksheets to single worksheet based on header criteria
    By nateaskins in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 03-21-2014, 10:42 AM
  4. [SOLVED] Copy entire row FROM ONE worksheet TO MULTIPLE worksheets IF criteria met
    By lukaskendera in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-27-2013, 01:04 PM
  5. [SOLVED] Macro to copy *PART* of a row into another worksheet if multiple criteria are met
    By as703 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-21-2013, 04:16 PM
  6. Copy data from one worksheet to another using multiple criteria
    By 01chopper in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-14-2012, 10:42 AM
  7. Copy rows from a column based on inputted search criteria to another worksheet
    By dale.lebar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-27-2012, 02:17 PM

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