+ Reply to Thread
Results 1 to 7 of 7

Thread: How do I copy a row from one worksheet to another by looking for a character string?

  1. #1
    Registered User
    Join Date
    01-25-2012
    Location
    Addison, TX
    MS-Off Ver
    Excel 2010
    Posts
    3

    How do I copy a row from one worksheet to another by looking for a character string?

    I need to look for a character string in the cells in row A. If the cell contains "GRA" I need to copy the row to the worksheet named "A". If the cell contains "GRB" I need to copy the row to the worksheet named "B" and so on. There can be multiple strings found in one cell. I have all of my worksheets named and I do not have control over how many rows there are in any of the worksheets. I am using Excel 2010.
    Attached Files Attached Files
    Last edited by businessanalyst; 01-27-2012 at 09:15 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    12-14-2009
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    986

    Re: How do I copy a row from one worksheet to another by looking for a character stri

    Because you work in the medical field, I put a little bit more effort haha... The only way I can think of is writing a macro. Take a look the file. Basically, the code is going to look at Column A row #2 to the last row. If the cell contains the key word, then it is going to copy the entire row to the destination worksheet.

    Sub Test()
    Dim LastRow As Long, i As Long
    Dim x As Long, y As Long
    Dim cell As Range
    Dim WSA As Worksheet, WSB As Worksheet, WS As Worksheet
    
    Set WS = Worksheets("Source")
    Set WSA = Worksheets("A")
    Set WSB = Worksheets("B")
    
    LastRow = WS.Cells(Rows.Count, 1).End(xlUp).Row
    x = 0
    y = 0
    For i = 2 To LastRow
        If WS.Cells(i, 1).Value Like "*GRA*" Then
            WS.Rows(i).Copy WSA.Rows(x + 1)
            x = x + 1
        ElseIf WS.Cells(i, 1).Value Like "*GRB*" Then
            WS.Rows(i).Copy WSB.Rows(y + 1)
            y = y + 1
        End If
    Next i
    
    End Sub
    Attached Files Attached Files
    To thank someone who has helped you, click on the star icon below their name.

    I hate reading

    Portfolio

    I need a job.
    I am young and incompetent

  3. #3
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: How do I copy a row from one worksheet to another by looking for a character stri

    JieJenn, since the column A strings may have multiple matching strings, I don't think you want to use ElseIf, I believe you want to run each test fully, the same row may copy to multiple sheets.

    For i = 2 To LastRow
        If WS.Cells(i, 1).Value Like "*GRA*" Then _
            WS.Rows(i).Copy WSA.Range("A" & Rows.Count).End(xlUp).Offset(1)
        If WS.Cells(i, 1).Value Like "*GRB*" Then _
            WS.Rows(i).Copy WSB.Range("A" & Rows.Count).End(xlUp).Offset(1)
    Next i
    Last edited by JBeaucaire; 01-25-2012 at 06:24 PM.
    _________________
    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!)

  4. #4
    Valued Forum Contributor
    Join Date
    12-14-2009
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    986

    Re: How do I copy a row from one worksheet to another by looking for a character stri

    Good call. Thanks.

    Per JBeaucaire's suggestion

    Sub Test()
    Dim LastRow As Long, i As Long
    Dim x As Long, y As Long
    Dim cell As Range
    Dim WSA As Worksheet, WSB As Worksheet, WS As Worksheet
    
    Set WS = Worksheets("Source")
    Set WSA = Worksheets("A")
    Set WSB = Worksheets("B")
    
    LastRow = WS.Cells(Rows.Count, 1).End(xlUp).Row
    x = 0
    y = 0
    For i = 2 To LastRow
        If WS.Cells(i, 1).Value Like "*GRA*" Then
            WS.Rows(i).Copy WSA.Rows(x + 1)
            x = x + 1
        End If
    
       If WS.Cells(i, 1).Value Like "*GRB*" Then
            WS.Rows(i).Copy WSB.Rows(y + 1)
            y = y + 1
        End If
    
    Next i
    
    End Sub
    Attached Files Attached Files
    Last edited by JieJenn; 01-25-2012 at 06:27 PM.
    To thank someone who has helped you, click on the star icon below their name.

    I hate reading

    Portfolio

    I need a job.
    I am young and incompetent

  5. #5
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: How do I copy a row from one worksheet to another by looking for a character stri

    I took out the X and Y variables because I imagine the OP is going to add numerous "checks", no need to have a separate "variable" for each sheet individually when an OFFSET() will drop the data into the next empty row.
    _________________
    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!)

  6. #6
    Registered User
    Join Date
    01-25-2012
    Location
    Addison, TX
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: How do I copy a row from one worksheet to another by looking for a character stri

    I want to thank you all for your help! This worked perfectly!

  7. #7
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: How do I copy a row from one worksheet to another by looking for a character stri

    Thanks for the feedback.

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
    _________________
    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!)

+ 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.2.0