+ Reply to Thread
Results 1 to 7 of 7

macro to copy rows for specific value in cell, only copies last row found in column

  1. #1
    Registered User
    Join Date
    09-29-2009
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    5

    macro to copy rows for specific value in cell, only copies last row found in column

    Hello....I'm trying to create a macro that will scan a selected column for a value, and if it finds a cell with the value I am looking for, it will copy the entire row. For example, I am looking for cells with 3051 and H2.

    1. 3051AABBSSH2
    2. 3051SJDIUS
    3. 3051SJHEISH3
    4. 3051SJSJSH2
    5. 3051SSJJJH2

    After running the macro, it should have rows 1, 4 and 5 copied. I was trying to use the following code:

    Public Sub seekAndFilter()
    Dim rng As Range

    For Each rng In Selection
    If ((InStr(1, rng.Value, "3051") <> 0 And InStr(1, rng.Value, "H2") <> 0)) Then
    rng.EntireRow.Copy
    End If
    Next
    End Sub

    The issue I run into is that it only will copy the last row that it finds with 3051 and H2, so in the above example, it copies 3051SSJJJH2 3 times. I am very new to making macros, so I'm sure it must be a dumb error I made. Any help would be much appreciated!
    Last edited by dtm8119; 10-15-2009 at 09:26 AM.

  2. #2
    Forum Contributor
    Join Date
    08-27-2009
    Location
    South Africa
    MS-Off Ver
    Office 2010
    Posts
    217

    Re: Working on making a macro to copy rows if a certain value is found in a select co

    will it always be the same value or would it change from time to time?

  3. #3
    Registered User
    Join Date
    09-29-2009
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Working on making a macro to copy rows if a certain value is found in a select co

    it will change time to time. The value in the cell will not always be the same...I just need to find all cells within a row that contain 3051 and H2.

  4. #4
    Forum Contributor
    Join Date
    08-27-2009
    Location
    South Africa
    MS-Off Ver
    Office 2010
    Posts
    217

    Re: Working on making a macro to copy rows if a certain value is found in a select co

    is this a temporary solution you are looking for?

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Working on making a macro to copy rows if a certain value is found in a select co

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Registered User
    Join Date
    09-29-2009
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: macro to copy rows for specific value in cell, only copies last row found in colu

    title updated....I'm looking for a permanent solution

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: macro to copy rows for specific value in cell, only copies last row found in colu

    You don't need unnecessary wording like macro to in the title

+ 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